python函数里调用外部变量
1、可以直接使用
2、但不能直接修改,除非函数里global变量
—1
a=1def f():print(a)f()
结果: 1
—2
a=1def f():a=a+1print(a)f()
错误
—3
a=1def f():global a a=a+1print(a)f()
结果: 2
时间:2023-02-07 10:33:58
python函数里调用外部变量
1、可以直接使用
2、但不能直接修改,除非函数里global变量
a=1def f():print(a)f()
结果: 1
a=1def f():a=a+1print(a)f()
错误
a=1def f():global a a=a+1print(a)f()
结果: 2