亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1972 | 回復: 1
打印 上一主題 下一主題

Python之global [復制鏈接]

論壇徽章:
1
數(shù)據(jù)庫技術版塊每日發(fā)帖之星
日期:2015-06-12 22:20:00
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 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程序設計版塊每日發(fā)帖之星
日期:2015-11-10 06:20:00每日論壇發(fā)貼之星
日期:2015-11-10 06:20:00程序設計版塊每日發(fā)帖之星
日期:2015-11-26 06:20:00程序設計版塊每日發(fā)帖之星
日期:2015-12-02 06:20:00黃金圣斗士
日期:2015-12-07 17:57:4615-16賽季CBA聯(lián)賽之天津
日期:2015-12-23 18:34:14程序設計版塊每日發(fā)帖之星
日期:2016-01-02 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-01-06 06:20:00每日論壇發(fā)貼之星
日期:2016-01-06 06:20:00
2 [報告]
發(fā)表于 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
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP