首页 > 文章列表 > python变量名的查找方法

python变量名的查找方法

Python 变量名
441 2022-08-07

1、查找变量名由内而外,分别是Local、Enclosing、Global、Builtin。

2、访问变量时,先找到本地变量,包裹函数外部函数内部变量,然后是全局变量,最后是内置变量。

实例

In [11]: i = "G"
In [12]: def test():
i = "L"
print i, "in locals"
....:
In [13]: test()
L in locals
In [14]: print i, "in globals"
G in globals

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。