Global usage in Python

Source: Internet
Author: User

1. Document Description

In the official api help document of python3.3.2, see the following section:

The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global.Names listed in a global statement must not be used in the same code block textually preceding that global statement.Names listed in a global statement must not be defined as formal parameters or in a for loop control target, class definition, function definition, or import statement.

It means:

The global statement is a statement that applies to the current entire code block. It is the identifier of a global variable. If a name is not defined in a local namespace, the corresponding global name is automatically used. it is impossible to manually specify a global name without a global name. the name that appears in global cannot be used in the code before global. the name that appears in global cannot be used as a form parameter, nor as a circular control object. It cannot be used in class definitions, function definitions, or import statements.

Differences from nonlocal keywords:

A global statement is used to indicate a specific variable as the global scope and rebind it. A nonlocal statement is used to specify a specific variable as a closed scope and rebind it.

2. instance description

def scope_test():    def do_local():        spam = "local spam"    def do_nonlocal():        nonlocal spam        spam = "nonlocal spam"    def do_global():        global spam        spam = "global spam"            spam = "test spam"    do_local()    print("After local assignment:", spam)    do_nonlocal()    print("After nonlocal assignment:", spam)    do_global()    print("After global assignment:", spam)    scope_test()print("In global scope:", spam)

Note: The instance code is from the official python3.3.2 documentation.

The running result is:


We can see that only the global variable value is output in code that is parallel to the definition method.

    def do_global():        global spam        spam = "global spam"
If the global keyword is removed, the following result is displayed:


Span is not defined.

Summary:

Global variables are used to allow us to use the variables returned by the function in a class or function and perform complex computation. Local variables of a function can only be used within a function. If you need to span different functions or classes, you need to return this value in the basic function, run the method in the next function to obtain the value for calculation. If the program is not complex, it can be solved in a class. Global variables save us a lot of time and memory space.


Other netizens on the use of global introduction: http://blog.csdn.net/nilxin/article/details/1523898


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.