python中全局变量的使用
test.py
--------------------------------------------------
import sys
username = "muzizongheng"
password = "xxxx"
def Login(u, p):
username = u
password = p
print("username: ", username)
print("password: ", password)
-------------------------------------------------
上述代码中的全局变量username和password并没有被改变,python默认函数或者方法内部赋值的变量没有必要公开,默认都是局部变量。
如果想要更改全局变量, 应改为
def Login(u, p):
global username
global password
username = u
password = p
print("username: ", username)
print("password: ", password)
即,应该用global关键字来声明全局变量
https://muzizongheng.blog.csdn.net/