Closure function
What is Closures
Closure: closing a closed (function of the internal function), is a package comprising (a reference to the inner function rather than a variable outer scope of global scope).
Closure means: a function of an internal function of the external reference, and not scope of global scope.
Two kinds of ways as a function parameter passing
As a function of a parameter passing mode: parameters used in the form of
def func(x):
print(x)
func(1)
func(1)
func(1)
1
1
1
Parameter passing as a function of two ways: packet function
def outter(x):
x = 1
def inner():
print(x)
return inner
f = outter(1)
f()
f()
f()
# 查看闭包的元素
print(F"f.__closure__[0].cell_contents: {f.__closure__[0].cell_contents}")
1
1
1
f.__closure__[0].cell_contents: 1
Application package closure function
Significance closure package: the function returns an object, not just a function object, outside the scope function also wrapped in a layer, which makes the function calls regardless of where, preferentially the outer layer wrapped their scope.
Application: delay calculation (turns out that we are mass participation, and now we are wrapped up), reptiles fields.
import requests
def get(url):
response = requests.get(url)
print(f"done: {url}")
get('https://www.baidu.com')
get('https://www.baidu.com')
get('https://www.baidu.com')
get('https://www.cnblogs.com/linhaifeng')
get('https://www.cnblogs.com/linhaifeng')
get('https://www.cnblogs.com/linhaifeng')
done: https://www.baidu.com
done: https://www.baidu.com
done: https://www.baidu.com
done: https://www.cnblogs.com/linhaifeng
done: https://www.cnblogs.com/linhaifeng
done: https://www.cnblogs.com/linhaifeng
The above approach is extremely complex, if we use the default parameters can only solve a URL, so we can consider the use of closures way.
import requests
def outter(url):
def get():
response = requests.get(url)
print(f"done: {url}")
return get
baidu=outter('https://www.baidu.com')
python = outter('https://www.python.org')
baidu()
baidu()
python()
python()
done: https://www.baidu.com
done: https://www.baidu.com
done: https://www.python.org
done: https://www.python.org
No reference decorator
What is the decorator
Device refers to the tool, and the function of the program is to have a functional tool, it refers to a decorator to add additional functionality to the decorator object.
Therefore, the definition of decorator is the definition of a function, but the function of the function is used to add additional functionality to other functions.
have to be aware of is:
- Decorator itself can actually be any object that can be called
- Ornamented objects can be any callable objects
Why use a decorator?
If we have a project on line, we need to modify a particular method, but we do not want to modify the method of use, this time you can use decorator. Because the software maintenance should follow the open closed principle that once the software on-line operation, maintenance and modification of software source code is closed, the extended function refers to the open.
Achieve decorator must follow two principles:
- Does not modify the source code of an object to be decorated
- Call does not modify the manner of decorative objects
Decorator in fact, add new features to the object to be decorated under the premise of following the above two principles.
Decorator syntax sugar
In the decorative function just above, and is written on a separate line@装饰器名
Decorative template
def deco(func):
def wrapper(*args,**kwargs):
res = func(*args,**kwargs)
return res
return wrapper
There are parameters decorator
No parameters set decorator only two layers, three sets to tell a decorator - there is a reference decorator, but now let's implement a registered user login decorators.
See nick blog