[Python]global and Nonlocal keywords

Source: Internet
Author: User

In Python, when referencing a variable, the search for the variable is based on local scope, nested scope (enclosing function locals), global scope (global), built-in scope (Builtins module) Order, that is, the so-called LEGB rule.
However, when assigning a value to a variable within a function, the variable is not first found according to the LEGB rule above, and then the variable is assigned a value. In Python, when assigning a value to a variable in a function, there is a rule like this:

"When assigning a variable name in a function (rather than referencing it in an expression), Python always creates or alters the variable name of the local scope unless it is already declared as a global variable in that function."

Let's look at an example:

= 99def func(): x = 88func()print(x) #输出99

The above program assigns X to 88 in the Func function, according to the above rule, because there is no variable x in the Func function, so python creates a variable x in the local scope of the Func function, which means that X is not x = 99 an X in This will also allow you to understand why the final program output is still 99.
If you want to modify the global variable x in a function instead of creating a new variable in the function, the keyword is used global , as shown here:

= 99def func() global x x = 88func()print(x) #输出88

The statements in the above program global x tell Python to use the variable x in the global scope within the local scope of the Func, so in the following x = 88 statement, Python no longer creates a new variable in the local scope, but instead directly references the variable x in the global scope, The final output of this program 88 is not difficult to understand.
Key sub nonlocal -functions are global similar to keywords, using nonlocal keywords to modify variables in nested scopes in a nested function, as in the following example:

def func () : Count = 1 def Span class= "token function" >foo () : Count = 12 Foo (print  (Count)  #输出1        

    The above program, in the nested Foo function, assigns a value to the variable count, also creates a new variable, rather than using the count in the count = 1 statement, If you want to modify count in a nested scope, you will use the nonlocal keyword:

def func () : Count = 1 def Span class= "token function" >foo () : nonlocal count count = 12 Foo (print  (Count)  #输出12        

In the above program, using the keyword in the foo function nonlocal tells Python to use the variable count in the nested scope in the Foo function, so modifying the variable count directly affects the count variable in the nested scope, and the program finally outputs 12.
Note: Variables that are decorated with keywords global may not exist before, and nonlocal variables decorated with keywords must already exist in the nested scope.

[Python]global and Nonlocal keywords

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.