python函数中变量LEGB原则
2013-03-21 12:26
288 查看
L-----local 局部名字空间 E-----enclosing 直接外围空间 G-----global 全局名字空间 B-----builtin 内建名字空间 变量名是按照这个顺序搜索的。 看下面这段程序:
x = 99 print("outside of function","x=",x) def funa(y): print("in the funa,x=",x) print("in the funa,y=",y) def funb(): print("in the funb,x =",x) print("in the funb,y =",y) z=x+y funb() return z print(funa(1)) print("outside of function","x=",x)
输出的结果为:
outside of function x= 99
in the funa,x= 99
in the funa,y= 1
in the funb,x = 99
in the funb,y = 1
100
outside of function x= 99
相关文章推荐
- Python 的变量作用域和 LEGB 原则
- Python 的变量作用域和 LEGB 原则
- Python 的变量作用域和 LEGB 原则
- 零基础学python-16.3 变量名解析:LEGB原则
- 零基础学python-16.3 变量名解析:LEGB原则
- python 变量名解析 LEGB原则
- Python 的变量作用域和 LEGB 原则
- python --- 变量和函数的作用域
- Python-同一个类两个函数间变量的调用
- python函数局部变量
- python入门基础教程06 python变量与函数
- Python在嵌套函数内部访问并父级函数的变量
- Python中定义“私有”成员变量和成员函数
- python内部函数如何修改外部函数变量
- 笨办法学python习题19 函数和变量
- Python在函数中使用全局变量的问题
- python 类的函数,类方法,静态方法,类变量,实例变量浅析
- 【Python】PYTHON 函数局部变量和全局变量
- python变量的legb作用域规则
- 详解Python函数作用域的LEGB顺序