1, variable scope
Python, variables in the program is not in any position that can be accessed, access depends on where the variable is assigned.
Scope of a variable determines which part of a program in which a particular variable name can be accessed. Python's scope, a total of four kinds, namely:
- L (Local) local scope
- E (Enclosing) function outside the closure function
- G (Global) global scope
- B (Built-in) built scope (where the scope of the built-in function module)
To L -> E -> G - Rules> B lookup, namely: not found locally, will find a local (e.g., closure) to the outer partial, you will not find to find the global, addition to built-in look.
= 0 g_count # global scope DEF Outer (): o_count =. 1 # functions outer closure function DEF Inner (): the i_count = 2 # local scope
Only Python module (Module1), class (class) and a function (def, lambda) before introducing a new scope, the other block of code (e.g., if / elif / else /, try / except, for / while, etc.) does not introduce a new scope, that variable defined within these statements, external can visit the following code:
if True: msg = 'I am from China' print(msg) -------------------------------------- 'I am from China'
Msg variable is defined in the example if block but still be accessible outside.
If msg is defined in the function, then it is a local variable, not external access:
def test(): msg_inner = 'I am from China' print(msg_inner) ---------------------------------------------------------- Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'msg_inner' is not defined
From the point of view of information being given, it explained msg_inner not defined, can not be used because it is a local variable, can be used only within the function.
2, global variables and local variables
It is defined as having a local scope, defined outside the function has global scope within a function of the variables.
The local variables can be declared within its access function, and can access the global variables in the entire program range. When you call a function, all variables declared within a function name will be added to the scope. Following examples:
Total = 0 # This is a global variable DEF SUM (arg1, arg2): # Returns the two parameters and. " Total = arg1 + arg2 # Total here is a local variable. Print ( " the function is a local variable: " , Total) return Total # Call the sum function sum (10, 20 ) Print ( " external function is a global variable: " , Total) --------------------------- ----------------------------- the function is a local variable: 30 external function is a global variable: 0
3, global and nonlocal keyword
When you want to modify the scope of internal variables outer scope, it is necessary to use a global and nonlocal keyword.
The following examples modify global variables num:
1 = NUM DEF fun1 (): global NUM # need to use the global keyword to declare Print (NUM) Surely = 123 print (whether) fun1() print (whether) ---------------------------------------------- ----------- 1 123 123
To modify nested scopes (the enclosing scope, the outer non-global scope) variables nonlocal keyword is required, as the following examples:
def outer(): num = 10 def inner(): NUM nonlocal # nonlocal keyword to declare a = 100 print (whether) inner() print (whether) outer() --------------------------------------------------- 100 100
There is also a special case
a = 10 def test(): a = a + 1 print(a) the Test () # error ------------------------------ Traceback (most recent call last): File "test.py", line 7, in <module> test() File "test.py", line 5, in test a = a + 1 UnboundLocalError: local variable 'a' referenced before assignment
Error message for the local scope misquoted, because the function of a test using a local, undefined, can not be modified.
Modifying a global variable, function parameters are passed through, the normal execution result is output:
a = 10 def test(a): a = a + 1 print(a) the Test (A) # error ---------------------------- 11