No self-starting individual public: Python programming time
without the author's permission, please do not reprint, the offender must be held accountable.
1. Scope
Python's scope can be divided into four categories:
- L (Local) local scope
- E (Enclosing) function outside the closure function
- G (Global) global scope
- B (Built-in) built scope
Search order variables / functions:
L -> E -> G -> B
Mean, it can not be found locally, they go outside the local scope of a local looking for (eg closures), and went to find the global domain could not find a job, and then could not find a built-in job field went looking for .
It will affect the variables / functions have scope
- Function: def or lambda
- Class: class
- Keywords: global noglobal
- Files: * py
- Derivation of formula: [], {}, (), etc., the only Py3.x, Py2.x variable leak occurs.
1, the previous assignment, after the reference
# ------同作用域内------
name = "MING"
print(name)
# ------不同作用域内------
name = "MING"
def main():
print(name)
2, the front reference, assigned after (same scope)
print(name)
name = "MING"
# UnboundLocalError: local variable 'name' referenced before assignment
3, in the lower assignment, quoted at the top
# L -> E -> G -> B
# 从左到右,由低层到高层
def main():
name = "MING"
print(name)
# NameError: name 'name' is not defined
2. Closure
Closures This concept is important Oh. You must master.
Definition of a function within an external function, the function in the use of temporary variables outside the function and the return value is outside the reference function. This constitutes a closure. In fact, decorative functions, many of which are closures.
It did not seem to understand why beginners will find it difficult to understand the closure of it?
I explain, you will understand.
In general, in our perception of them, if a function is over, everything will be relieved of internal functions, returned to memory, local variables will disappear. But the closure is a special case, if the external function found at the end of their temporary variables inside the function will be used in the future, put the temporary variable is bound to an internal function, and then ended his own again.
You can look at the following code, constitutes closure. Including variable functions may reference external function.
def deco():
name = "MING"
def wrapper():
print(name)
return wrapper
deco()()
# 输出:MING
3. Change Scope
Position variable scope, its definition (or assignment) are related, but not absolutely relevant.
Because we can to change to some extent 向上
the role of the range.
-
Keywords: global
will become global variables local variables -
Keywords: nonlocal
can function in a closure, the closure referenced using an external variable function (non-global Oh)
global well understood, they talk down here nonlocal.
First look at an example
def deco():
age = 10
def wrapper():
age += 1
return wrapper
deco()()
Running about, I will complain.
# UnboundLocalError: local variable 'age' referenced before assignment
But so OK
def deco():
age = 10
def wrapper():
nonlocal age
age += 1
return wrapper
deco()()
# 输出:11
In fact, if you do not use +=
, -=
such as the operation of a class, without nonlocal does not matter. This shows the characteristics of the closures.
def deco():
age = 10
def wrapper():
print(age)
return wrapper
deco()()
# 输出:10
1.5.4 set of variables
In Python, there are two built-in functions, you may not use, but they need to know.
- globals (): dict way to store all global variables
- locals (): stored in a dict of all local variables
globals()
def foo():
print("I am a func")
def bar():
foo="I am a string"
foo_dup = globals().get("foo")
foo_dup()
bar()
# 输出
# I am a func
locals()
other = "test"
def foobar():
name = "MING"
gender = "male"
for key,value in locals().items():
print(key, "=", value)
foobar()
# 输出
# name = MING
# gender = male