python能够改变变量作用域的代码段是def、class、lamda.
变量搜索路径是:本地变量->全局变量
python的变量命和内存的对应关系是会改变的,c/c++是不变的
python的全局变量在函数内不能做左值:
a = 100
def test():
c = a + 10 #正确
#a = a + 10 #error
print(c)
test()
如果要在函数内修改全局变量,需要使用global关键字:
a = 100
def test():
global a
a = a + 10
print(a)
test()