Python function-Variable scope __ function

Source: Internet
Author: User
Tags variable scope

1. The scope of the code in which variables function is referred to as variables, and the variables in different scopes do not affect each other. The variables defined inside the function are local variables, and the variables defined outside the function are global variables.


2. A local variable is valid only in the function that defines it, and is considered to be another variable even if it uses a variable of the same name outside the function.



3. Global variables are valid in the defined code, including the function body defined later, with the same value as the variable with the same name as the where function body and outside the function body, which is an implicit global variable. If the global variable has the same name as the local variable, then in the function that defines the local variable, only the local variable is valid, that is, the local variable overrides the global variable with the same name.


4. If a variable has been defined outside the function, the value of the variable needs to be modified within the function, which can be declared as a global variable by the global keyword within the function. If a variable is not defined outside the function, a variable is declared as a global variable directly using the Global keyword within the function, and a new global variable is automatically added after the function is called.


5. When the function is finished, local variables defined within the function are automatically deleted and inaccessible, while global variables defined within the function are still accessible.


6. In general, local variables are referenced faster than global variables and should be considered as a priority. Unless it is really necessary, you should avoid using global variables, because global variables increase the implicit coupling between different functions, reducing code readability, making code testing and correcting difficult.


7. If you need to share global variables between different modules of a program, you can write a specialized module to do this.

For example, the following variable definitions are available in the module a.py:

A= 0


In the module b.py, the following statement is included:

Import A

A.A = 1

The following statement is available in the module c.py:

Import A

Print (A.A)

Thus, global variables are shared among different modules.



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: [email protected] 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.