Python local variables and global variables

Source: Internet
Author: User

When you declare variables in a function definition, they have nothing to do with other variables that have the same name outside the function, that is, the variable name is local to the function. This is called the scope of the variable. All variables are scoped to their defined blocks, starting with the point where their names are defined.


using local VariablesExample: func_local.py#!/usr/bin/python#encoding: Utf-8 def func (x):print ' x is ', xx=2print ' Changed local x to ', X x=50func (x)print ' x is still ', x Execute func_local.py# python func_local.pyx is 50Changed local x to 2x is still 50 parsing: In the function, the first time we use the value of X, Python uses the value of the formal parameter declared by the function. Next, we assign the value 2 to x,x as a local variable of the function. So, when we change x within a function, the x defined in the main block is unaffected. In the last print statement, we proved that the value of x in the main block was not really affected. using Globe statementsIf you want to assign a value to a variable that is defined outside the function, then you have to tell Python that the variable name is not local, and GlobalOf We use the global statement to complete this function. Without the global statement, it is impossible to assign a value to a variable that is defined outside the function. You can use the values of variables defined outside the function (assuming there are no variables with the same name within the function). However, I do not encourage you to do so, and you should try to avoid doing so, as this makes the reader of the program unclear where the variable is defined. Using the global statement makes it clear that the variable is defined outside the block. Example: func_global.py#!/usr/bin/python#encoding: Utf-8 def func ():Global xprint ' x is ', xx=2print ' Changed local x to ', X x=50func ()print ' Value of x is ', xExecute func_global.py# python func_global.pyx is 50Changed local x to 2Value of X is 2 parsing: The global statement is used to declare that X is global-so when we assign a value to x within a function This change is also reflected when we use the X value in the main block. You can use the same global statement to specify multiple global variables. For example, Global X, y

Python local variables 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.