Python local and global variables

Source: Internet
Author: User
Global and local variables although simple, but still record, mainly reproduced reprinted from: Http://blog.sina.com.cn/s/blog_436992740102ux8z.html First look at a piece of code: visible Old_price is a global variable, final_price is the result of a local variable running:

Local variables get outside. If there is no definition, then the global variable can be used in the DEF function?

Operation Result:
Visible, the DEF-defined function can print global variables if you want to make changes to global variables in functions defined by Def, you should write this:
The global declaration of Old_price in the function must be written, otherwise, when the function of the old_price is changed, the system found that the name and the global variable has a name is the same, it will be in the function of the old_price as a newly established local variable, then, The first print in the function will go wrong because there is no definition at this time that Old_price,old_price is defined by the phrase = 50. Run Result: The result of the operation is known: The value of the global variable has been changed in the function.

Python local and global 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.