欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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 

非常简明