您的位置:首页 > 编程语言 > Python开发

python 全局变量规则

2017-11-18 12:39 225 查看
Section 1
mValue = "1"
def fun1():
if(mValue=="1"):
print(mValue)
运行正常Section 2
def fun2():
if(mValue=="1"):
mValue = "hah";
print(mValue)
IDE检测不过Section 3
def fun2():
global mValue;
if(mValue=="1"):
mValue = "hah";
print(mValue)
运行正常
总结: (1)在方法中可以直接打印全局变量
(2)在方法中修改全局变量的值需要先global申明
====================================================补充:可变类型的全局变量不使用global也可以修改
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python