[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]

 

posted @ 2021-07-29 09:05  cxc1357  阅读(30)  评论(0编辑  收藏  举报