Local and global variables of PYTHON functions, python global variables

Source: Internet
Author: User

Local and global variables of PYTHON functions, python global variables

With such a piece of PYTHON code, anyone who is engaged in C Language Development knows that if global variables are defined and function variables with the same name are not defined in the function, the value assigned to the variable in the function is the modification of the Space Value of the global variable,

But they are different in PYTHON. in python functions, if they are not explicitly declared as global variables, python will create local variables for the functions, therefore, the variable operated in the function is also a local variable.

Count = 5def myfun (): count = 10 print (count) myfun () print (count)

The printed result is as follows:

Then, how can we modify the value of the global variable in the function? Here we use a keyword: global. modify the code as follows to get the correct value.

Count = 5def myfun (): global count = 10 print (count) myfun () print (count)

 

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.