[Python] 模块调用

在t_1中定义全局变量a,t_2调用t_1函数,观察a的变化

t_1:

a = 0
b = []
def f():
    global a
    a += 1
    b.append(1)

函数中的a需要声明global,否则会报错,b不需要声明

t_2:

import t_1
t_1.f()
t_1.f()
print(t_1.a)
print(t_1.b)

结果为2,[1,1]

原文地址:https://www.cnblogs.com/cxc1357/p/10424949.html