python variable definitions and variable life cycle

first part

Recently I wrote Python found a fun time phenomenon, that is, if else redefinition of variables, not declared globally, can also be used outside,

Here involves the issue of a python variable life cycle.

python can change the code segment is variable scope def, class, lamda.

if / elif / else, try / except / finally, for / while does not involve changing the variable scope,

That is their code block variable is accessible externally

Variable search path is: local variables -> Global Variables

https://img-blog.csdn.net/20161110105647895?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

http://blog.csdn.net/u012965373/article/details/53113586

the second part

python, some different provisions for variable scope. Such as C / C ++, java and other programming languages, the default is inside the function can directly access the global variables defined outside the function, but it will be a problem in python, here is an example.


    
    
  1. test.py:
  2. #!/usr/bin/python
  3. COUNT= 1
  4. def func () ;
  5. COUNT = COUNT + + 1
  6. func()
  7. Python test.py, will run error:
  8. Traceback (most recent call last):
  9. File "test.py", line 8, in
  10. func()
  11. File "test.py", line 6, in func
  12. COUNT = COUNT + + 1
  13. UnboundLocalError: local variable 'COUNT' referenced before assignment

" UnboundLocalError: local variable 'COUNT' referenced the before the Assignment " mean COUNT variable is referenced before assignment.

Here you must know python and other programming languages is not the same place. Like programming languages C / C ++ and the like, the variable name is actually a memory area on behalf of , the variable assignment, which means that the new value into the variable specified memory area . For python, all variables are references to the memory region, the equivalent of the variable assignment variables referenced memory change from one area to another area to store a new value . That is, C / C ++, the correspondence relationship between the variable names and memory area does not change, the only change corresponding value stored in memory; and in python, storing the value of a variable in a memory area of a reference variable value change is not because the value of the variable memory area pointed to by the changes, but variable references a new area of memory to store the new value. All variables in python are not variable corresponding to variations in java, any change in the value of a variable reference corresponds with a change of the memory area. Figure 1 differences are as follows:

\

Comparative FIG 1 variable

python has an id function, the python has an id function, Help (id) can see its description, is as follows:


    
    
  1. Help on built- in function id in module __builtin__:
  2. id(...)
  3. id( object) -> integer
  4. Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects.(Hint: it 's the object's memory address.)
  5. ( END)

Simply put, id reaction function is the memory address of the object, see the following results:

test.py:
#!/usr/bin/python

COUNT = 1
for i in range(5):
COUNT = COUNT + 1
print id(COUNT)

python test.py result:
11,031,328
11,031,304
11.03128 million
11,031,256
11,031,232

Match here and on the map above described, python in each assignment are variable references the memory space has changed.

Back to Top "referenced before assignment" error, the reason why this error occurs because the python found for assignment COUNT variable, it is added to the local namespace function (actually function, which is run in function prior to assignment to occur). When assignment, the right side of the assignment operator COUNT variable is referenced, but this time only the variable COUNT is added to the local namespace function, without being specifically assigned, so the above error occurs. In fact, the problem here lies in the place of assignment, because there are assignment led to the variable is added to the local namespace function. If there is no assignment, but the variable is referenced, is no problem, as follows:


  
  
  1. test.py:
  2. #!/usr/bin/python
  3. COUNT= 1
  4. def func () ;
  5. temp = COUNT + 1
  6. print "temp:",COUNT
  7. print "COUNT:",COUNT
  8. func()
  9. python test.py operating results:
  10. temp: 1
  11. COUNT: 1

In this way, COUNT variable is not added to the local namespace functions, python interpreter does not find it in the local namespace function, and then, python interpreter will continue to look at the global namespace, results in the global namespace COUNT and find the definition of reference its value, so the program runs without any problems.

Here you might ask, do not change the value of a global variable in a function do? No, if you want to modify the value of a global variable in the function, the function will be carried out in the global variable declaration to tell the python interpreter, the variables are global namespace, as follows:


  
  
  1. test.py:
  2. #!/usr/bin/python
  3. COUNT= 1
  4. def func () ;
  5. global COUNT
  6. COUNT = COUNT + 1
  7. print "COUNT:",COUNT
  8. func()
  9. python test.py operating results:
  10. COUNT: 2

Guess you like

Origin blog.csdn.net/Mei_ZS/article/details/83620514