免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1953 | 回复: 1
打印 上一主题 下一主题

Python之global [复制链接]

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-06-12 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-11 10:09 |只看该作者 |倒序浏览
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.
  1. X = 88                         # Global X

  2. def func():
  3.     global X
  4.     X = 99                     # Global X: outside def

  5. func()
  6. print(X)                       # Prints 99
复制代码
  1. y, z = 1, 2                    # Global variables in module

  2. def all_global():
  3.     global x                   # Declare globals assigned
  4.     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
  1. # thismod.py
  2. var = 99                              # Global variable == module attribute

  3. def local():
  4.     var = 0                           # Change local var

  5. def glob1():
  6.     global var                        # Declare global (normal)
  7.     var += 1                          # Change global var

  8. def glob2():
  9.     var = 0                           # Change local var
  10.     import thismod                    # Import myself
  11.     thismod.var += 1                  # Change global var

  12. def glob3():
  13.     var = 0                           # Change local var
  14.     import sys                        # Import system table
  15.     glob = sys.modules['thismod']     # Get module object (or use __name__)
  16.     glob.var += 1                     # Change global var

  17. def test():
  18.     print(var)
  19.     local();
  20.     print(var)
  21.     glob1();
  22.     print(var)
  23.     glob2();
  24.     print(var)
  25.     glob3()
  26.     print(var)
复制代码
run and get results
  1. >>> import thismod
  2. >>> thismod.test()
  3. 99
  4. 99
  5. 100
  6. 101
  7. 102
复制代码

论坛徽章:
26
2015亚冠之胡齐斯坦钢铁
日期:2015-06-25 21:40:202015亚冠之柏斯波利斯
日期:2015-08-31 17:03:192015亚冠之柏斯波利斯
日期:2015-11-07 13:10:00程序设计版块每日发帖之星
日期:2015-11-10 06:20:00每日论坛发贴之星
日期:2015-11-10 06:20:00程序设计版块每日发帖之星
日期:2015-11-26 06:20:00程序设计版块每日发帖之星
日期:2015-12-02 06:20:00黄金圣斗士
日期:2015-12-07 17:57:4615-16赛季CBA联赛之天津
日期:2015-12-23 18:34:14程序设计版块每日发帖之星
日期:2016-01-02 06:20:00程序设计版块每日发帖之星
日期:2016-01-06 06:20:00每日论坛发贴之星
日期:2016-01-06 06:20:00
2 [报告]
发表于 2015-06-11 12:09 |只看该作者
一直都不知道还有这个方法 看到这个帖子才知道 谢谢

import thismod                    # Import myself
import sys                        # Import system table
glob = sys.modules['thismod']     # Get module object (or use __name__)


呵呵,也偷偷的学习一下

script ==> thismod.py
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP