python 全局变量使用(1)
程序员文章站
2024-01-21 23:23:22
...
x = 50
def func():
global x
print('This function is now using the global x!')
print('Because of global x is: ', x)
x = 2
print('Ran func(), changed global x to', x)
print('Before calling func(), x is: ', x)
func()
print('Value of x (outside of func()) is: ', x)
结果
Before calling func(), x is: 50
This function is now using the global x!
Because of global x is: 50
Ran func(), changed global x to 2
Value of x (outside of func()) is: 2
非常简明
上一篇: python 全局变量的使用