Scope of global and nonlocal in Python

Source: Internet
Author: User

Python refers to the order of variables: the current scope local variable, the outer scope variable, and the global variable->python built-in variables in the current module.

A global

The global keyword is used to use globals in functions or other local scopes. However, if you do not modify global variables, you can not use the Global keyword.

1 gcount = 023def  global_test ():4     gcount+=1  5     print  (gcount)6 global_test ()

D:\Python34\python.exe e:/pycharmprojects/day3/globaltest.py
Traceback (most recent):
File "e:/pycharmprojects/day3/globaltest.py", line 6, <module>
Global_test ()
File "e:/pycharmprojects/day3/globaltest.py", line 4, in Global_test
Gcount+=1
Unboundlocalerror:local variable ' gcount ' referenced before assignment

Process finished with exit code 1

The first line defines a global variable (you can omit the global keyword).

In the Global_test function, the program will be because "if the intrinsic function has the same name variable or global variable that references the external function, and the variable is modified." Then Python will think of it as a local variable, and because there is no gcount definition and assignment in the function, an error is given.

Second, declare global variables, if you want to modify the global variable locally, you need to declare the global variable at the same time:

Gcount = 0def global_test ():    global  gcount    gcount+=1    print (Gcount) global_test ()

If you declare gcount as a global variable in a function, you can modify it. Correct output 1.

Third, in the local if the global variable is not declared, and the global variable is not modified. You can use global variables normally:

Gcount = 0def global_test ():    print (Gcount) global_test ()

If the global variable is not modified locally, the program outputs 0 correctly.

The nonlocal keyword is used to use an outer (non-global) variable in a function or other scope.

Def make_counter ():      count = 0      def counter ():          nonlocal count          count + = 1          return count      return Counter        def make_counter_test (): MC = Make_counter () print (MC ()) Print (MC ()) Print  (MC ()) Make_counter_test ()

Output:

1

2

3

Five

Def scope_test ():    def do_local ():        spam = "local spam" #此函数定义了另外的一个spam字符串变量, and the life cycle is only within this function. Here the spam and outer spam is two variables, if write spam = spam + "Local spam" will error    def do_nonlocal ():        nonlocal  spam        #使用外层的spam变量        spam = "nonlocal spam"    def Do_global ():        global spam        spam = "Global spam"    spam = "Test spam"    do _local ()    print ("After local assignmane:", spam)    do_nonlocal ()    print ("After nonlocal assignment:", spam)    Do_global ()    print ("After global assignment:", spam) Scope_test () print ("In global scope:", spam)

The output is:

After local assignmane:test spam
After nonlocal assignment:nonlocal spam
After global assignment:nonlocal spam
In global scope:global spam

Scope of global and nonlocal in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.