Python References global variables

Source: Internet
Author: User

1.global is called a keyword in python, not a command

The role of 2.global is to refer to global variables to local scopes

3. If there is not a variable A in the global variable and a statement such as Globa A is written in the function, then a variable a is created in the global variable

4. code example:

########################################################################

a=10

def fun ():

Global A

A = 20

Fun ()

Print (a)

Summary: A general example of referencing global variables in a function, that is, a local scope

#########################################################################

def fun ():

Global A

A = 20

Fun ()

Print (a)

Summary: This code because there is no global variable A, and the function refers to the variable A, the subsequent execution of the SUM function, so print out the global variable A

############################################################################

"Key Notes"

When will the global general be used? This is useful when you want to change global variables in a local scope.

--------------------------------------------------------------------------------------------------------------- ------------------

a=10

def fun ():

Global A

A = 20

A=a+30

Fun ()

Print (a)

Summary: This code will end up typing 40, because in the local scope has changed the global variable a value, the global variable in a is not the previous 10, now is the modified 40

Python References global variables

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.