Python之global
2015-06-10 19:52
666 查看
1 Global
The global statement and its nonlocal cousin are the only things that are remotely like declaration statements in Python. They are not type or size declarations; they are namespace declarations. The global statement tells Python that a function plans to change one or more global names.
• Global names are variables assigned at the top level of the enclosing module file.
• Global names must be declared only if they are assigned within a function.
• Global names may be referenced within a function without being declared.
In other words, global allows us to change names that live outside a def at the top level of a module file.
2 Example
The global statement consists of the keyword global, followed by one or more names separated by commas.
x, y, and z are all globals inside the function all_global. y and z are global because they aren’t assigned in the function; x is global because it was listed in a global statement to map it to the module’s scope explicitly. Without the global here, x would be considered local by virtue of the assignment.
3 Access globals
run and get results
The global statement and its nonlocal cousin are the only things that are remotely like declaration statements in Python. They are not type or size declarations; they are namespace declarations. The global statement tells Python that a function plans to change one or more global names.
• Global names are variables assigned at the top level of the enclosing module file.
• Global names must be declared only if they are assigned within a function.
• Global names may be referenced within a function without being declared.
In other words, global allows us to change names that live outside a def at the top level of a module file.
2 Example
The global statement consists of the keyword global, followed by one or more names separated by commas.
X = 88 # Global X def func(): global X X = 99 # Global X: outside def func() print(X) # Prints 99
y, z = 1, 2 # Global variables in module def all_global(): global x # Declare globals assigned x = y + z # No need to declare y, z: LEGB rule
x, y, and z are all globals inside the function all_global. y and z are global because they aren’t assigned in the function; x is global because it was listed in a global statement to map it to the module’s scope explicitly. Without the global here, x would be considered local by virtue of the assignment.
3 Access globals
# thismod.py var = 99 # Global variable == module attribute def local(): var = 0 # Change local var def glob1(): global var # Declare global (normal) var += 1 # Change global var def glob2(): var = 0 # Change local var import thismod # Import myself thismod.var += 1 # Change global var def glob3(): var = 0 # Change local var import sys # Import system table glob = sys.modules['thismod'] # Get module object (or use __name__) glob.var += 1 # Change global var def test(): print(var) local(); print(var) glob1(); print(var) glob2(); print(var) glob3() print(var)
run and get results
>>> import thismod >>> thismod.test() 99 99 100 101 102
相关文章推荐
- 学习笔记:python
- python爬虫解决百度贴吧登陆验证码问题
- Python 的stat 模块
- 用Python写了一个postgresql函数,感觉很爽
- 用Python写了一个postgresql函数,感觉很爽
- python:re.sub简要分析
- Python基础:25文件
- Python基础:24with语句
- Python基础:23异常
- Python学习igraph
- 用python实现本地文件搜索
- [Python]项目打包:5步将py文件打包成exe文件
- Python进阶(下)
- 【Python】Python Requests快速入门
- python django web项目的构建步骤(一)
- 关于python中文输出乱码的处理
- Python 中的 classmethod 和 staticmethod 有什么具体用途?
- Python进阶(上)
- Debug on <python for android> or <kivy>
- Python基础(下)