Reading catalogue
I. Decorators
1. The Concept of Decorator
# Decorator Definition: Essentially function, function is to add additional functions to other functions
2. Principles to be followed in decoration
The principle of " 1. Do not modify the source code of the modified function 2. Calling method of modified function without modification Decorator The device of another person can be any callable object or the decorated person can be any callable object. # Emphasizing the principles of decoration: 1. Do not modify the source code of the decorated object 2. Do not modify the invocation of decorated objects # Objectives of Decorators: Added new functionality to the decorated object on the premise of following the principles of 1 and 2
3. Achieving Knowledge Reserve of Decorators
Decorator = higher-order function + function nesting + closure
4. Higher Order Functions
Definition of higher-order functions: 1. The parameter received by a function is a function name. 2, the function Return value is a function name 3. Any of the above conditions can be called higher order functions.
5. Function nesting
def father(name):
print('from father %s' %name)
def son():
print('from the son')
def grandson():
print('from the grandson')
grandson()
son()
father('Zhu Rui')
Six, closure
1, closure
def father(name):
print('from father %s' %name)
def son():
print('from the son')
def grandson():
print('from the grandson')
grandson()
son()
father('Zhu Rui')
'''
//closure
'''
def father(name):
def son():
# name='simon1'
print('My father is%s' %name)
def grandson():
print('My grandfather is%s' %name)
grandson()
son()
father('simon')
2. Basic Realization of Functional Closed Packaging Ornaments
import time
def timmer(func):
def wrapper():
# print(func)
start_time=time.time()
func() #It's running test()
stop_time=time.time()
print('The running time is%s' %(stop_time-start_time))
return wrapper
@timmer #Grammar sugar, that's the point.
def test():
time.sleep(3)
print('test Function finished running')
# res=timmer(test) #Returns the address of wrapper
# res() #wrapper()
# test=timmer(test) #Returns the address of wrapper
# test() #wrapper()
test()
'''
//Grammatical sugar
'''
# @timmer #It's equivalent to test=timmer(test)
3. Function closure plus return value
#No return value added
import time
def timmer(func):
def wrapper():
# print(func)
start_time=time.time()
func() #It's running test()
stop_time=time.time()
print('The running time is%s' %(stop_time-start_time))
return 123
return wrapper
@timmer #Grammatical sugar
def test():
time.sleep(3)
print('test Function finished running')
return 'This is test Return value'
res=test() #It's running wrapper.
print(res)
//The results are as follows:
C:\Python35\python3.exe G:/python_s3/day20/Add the return value.py
test Function finished running
//Running time is 3.000171661376953
123
#Add the return value
import time
def timmer(func):
def wrapper():
# print(func)
start_time=time.time()
res=func() #It's running. test() ##Major modifications here 1
stop_time=time.time()
print('The running time is%s' %(stop_time-start_time))
return res ##Modify here 2
return wrapper
@timmer #Grammatical sugar
def test():
time.sleep(3)
print('test Function finished running')
return 'This is test Return value'
res=test() #It's running wrapper.
print(res)
//The results are as follows:
C:\Python35\python3.exe G:/python_s3/day20/Add the return value.py
test Function finished running
//Running time is 3.000171661376953
//This is the return value of test.
4. Function closures plus parameters
import time
def timmer(func):
def wrapper(name,age): #Add parameters, name,age
# print(func)
start_time=time.time()
res=func(name,age) ##Add parameters, name,age
stop_time=time.time()
print('The running time is%s' %(stop_time-start_time))
return res
return wrapper
@timmer #Grammatical sugar
def test(name,age): #Add parameters, name,age
time.sleep(3)
print('test Function finished running,The name is%s],Age is.%s]' % (name,age))
return 'This is test Return value'
res=test('simon',18) #It's running wrapper.
print(res)
Using variable length parameter code is as follows: the effect is flexible reference.
import time
def timmer(func):
def wrapper(*args,**kwargs): #test('simon',18) args=('simon') kwargs={'age':18}
# print(func)
start_time=time.time()
res=func(*args,**kwargs) #It's running test () func (*('simon'), **{age': 18})
stop_time=time.time()
print('The running time is%s' %(stop_time-start_time))
return res
return wrapper
@timmer #Grammatical sugar
def test(name,age):
time.sleep(3)
print('test Function finished running,The name is%s],Age is.%s]' % (name,age))
return 'This is test Return value of'
def test1(name,age,gender):
time.sleep(1)
print('test Function finished running,The name is%s],Age is.%s],Gender is.%s]' % (name,age,gender))
res=test('simon',18) #It's running wrapper.
print(res)
test1('simon',18,'male')