Small ape python circle of global and local variables

I believe understood the python's friends heard of global and local variables, right? Today follow a small circle ape pace of learning together about the python variable scope issue.

the python variable scope into local variables and global variables.

One, referred to as

L: local, local scope, i.e. the functions defined variables;

E: enclosing, nested local scope parent function, i.e. a function containing higher local scope of this function, but not global;

G: global, the global variable is a variable defined in the module level;

Second, location

Variables defined in the function generally only used internally function, which is a local variable.

Variables to the top of a document may be defined by any of the function call in the file, these variables can be used for the entire procedure is referred to as global variables

Third, the application

(1) declares a new local variable. If this variable name and the names of all the variables the same, then the local variable name overrides the global variable name.

# . 1 
NUM = 100 DEF FUNC (): 
    newValue = 45   # a new local variable newValue Print (newValue) 
FUNC ()   # output a local variable newValue i.e. new local variable # 2 
NUM = 100 DEF FUNC (): 
    NUM = 45   # declare a new local variable num, although all the variables of the same name and the outside world but it is a new local variable and is valid. He blocked all the variables of the same name out of Print (NUM) 
FUNC ()   # output a local variable num = 45 i.e. new local variable.




    








    

(2) to modify global variables in local variables,

= 100 NUM 
 DEF FUNC (): 

    NUM = 45     # could have been your intention is to be assigned to a global variable Num, but here are resolved to declare a new local variable NUM 

    Print (id (NUM))     # id new id 

FUNC ()   # output a local variable newValue i.e. new local variable 

Print (ID (NUM))

Then how to modify a global variable assignment in a local variable?

# 1 
Num = 100 DEF FUNC (): , Ltd. Free Join Num   # declare the Num is global. If you already have this global variable Num variable that refers to the global if it is not the Num it a new definition of global variables. 
    Num   = 200   # Those who function within the meaning Num always refers to global variables. You can not have a local variable called Num of. Print (Num) 
FUNC () Print (Num) # output is 200 Modify the global variables # 2 DEF FUNC (): , Ltd. Free Join Num   # declare the Num is global. And it is precisely at this time is not a global variable Num. So if the global Num it is not the new definition of a global variable Num. 
    Num   = 200 Print (Num) 
FUNC () Print (Num) # output 200 illustrates a definition of a new global variable Num



    


    








    


    


Simple local and global, from the general definitions can be judged directly, if it involves a local variable to modify the global variable, be sure to use the global keyword; I believe that after you read, global and local variables insight into , want to learn more python content, you can go to a small circle ape follow alex large coffee study, there will be a great harvest.

Guess you like

Origin www.cnblogs.com/xiaoyuanquan/p/10973327.html