Python learns the scope of 7--variables

Source: Internet
Author: User

1. Scope of variables
(1) Variables defined within a function include arguments, function only within the scope of the functions, and local variables are similar.
(2) Global variables, inside functions can be called outside the function, even if the variable is defined after the function.
Note: Local variables and global variables are defined in the same way as other languages.
If you modify a global variable within a function Zjh,python creates a name for the global variable within the function.
Substitution of local variables. Therefore, changes to global variables within a function do not affect global variables.
Note: How to make changes to global variables work inside a function.
This global variable is defined inside the function with the GLOBALS keyword.
Example: Before use:

    

After use:
    

Python learns the scope of 7--variables

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.