标签:reference 错误 assign python global oca 不能 error: 操作
函数作用域分为
1. 全局 在整个类和模块内有作用
2.局部 在函数内部优先于全局
在函数内部可以调用全局变量,但是不能修改其值(因为python中定义和修改操作一致),这是需要用到 globle 声明操作变量为全局变量
e.g. 正确:
hehe=6
def f():
global hehe
print(hehe)
hehe=3
f()
print(hehe)
e.g. 错误:
hehe=6
def f():
print(hehe)
hehe=2
f()
print(hehe)
# 会报错: 未定义变量 UnboundLocalError: local variable ‘hehe‘ referenced before assignment
标签:reference 错误 assign python global oca 不能 error: 操作
原文地址:http://www.cnblogs.com/lihuanghe/p/7921617.html