Python之global

來源:互聯網
上載者:User

標籤:

 

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 = 88                         # Global Xdef func():    global X    X = 99                     # Global X: outside deffunc()print(X)                       # Prints 99

  

y, z = 1, 2                    # Global variables in moduledef 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.pyvar = 99                              # Global variable == module attributedef local():    var = 0                           # Change local vardef glob1():    global var                        # Declare global (normal)    var += 1                          # Change global vardef glob2():    var = 0                           # Change local var    import thismod                    # Import myself    thismod.var += 1                  # Change global vardef 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 vardef test():    print(var)    local();    print(var)    glob1();    print(var)    glob2();    print(var)    glob3()    print(var)

  run and get results

>>> import thismod>>> thismod.test()9999100101102

 

Python之global

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.