Decorator
What is a decorator?
Decorator is a function, mainly used to wrap another function or class
The purpose of the wrapper is to change or add the function of the wrapped object without changing the original function name (or class name)
Function decorator
Decorator is a function, passed in is a function, returned is a function
Syntax:
Def decorator function name (parameter):
Statement block
Return function object
@Zhang Shiqi's function name
Def function name (parameter list):
Statement block
#This example shows the definition of decorator function and decorator to decorate another function
The grammar of Wei
def mydeco(fn):
def fx():
print("++++++++++++++++")
print('----------------')
return fx
def myfunc():
'' this function will be decorated as' '
Print ("myfunc called")
#The principle is to let myfunc rebind the function returned by mydeco
myfunc = mydeco(myfunc)
myfunc()
myfunc()
myfunc()
#This example shows the definition of decorator function and decorator to decorate another function
The grammar of Wei
def mydeco(fn):
def fx():
print("++++++++++++++++")
print('----------------')
return fx
@mydeco
def myfunc():
'' this function will be decorated as' '
Print ("myfunc called")
#@ mydeco on myfunc is equivalent to
#Add myfunc = mydeco (myfunc) here
myfunc()
myfunc()
myfunc()
#This example shows the definition of decorator function and decorator to decorate another function
The grammar of Wei
def mydeco(fn):
def fx():
print("++++++++++++++++")
Fn() ාcall previous decorated function
print('----------------')
return fx
@mydeco
def myfunc():
'' this function will be decorated as' '
Print ("myfunc called")
myfunc()
myfunc()
myfunc()
#This example shows the purpose and function of the decorator
#Simulation bank project
#Business: saving and withdrawing money
#----- adorner written in lower case by Xiao Li-----
def privileged_check(fn):
def fx(n, yuan):
Print ("checking permissions..... OK!")
FN (n, yuan) call the decorated function
return fx
def send_message(fn):
def fy(n, x):
fn(n, x)
Print ("sending to..., n)
return fy
# ------
@privileged_check
def savemoney(name, x):
Print (name, 'deposit', x, 'Yuan')
@privileged_check
@send_message
def withdraw(name, x):
Print (name, "withdraw money", x, "Yuan")
# ----------
Savemoney ("Xiao Wang", 200)
Savemoney ("Xiaozhao", 400)
Withraw ("small money", 500)
Document string for function
The string assigned to any variable for the first time in the function is the document word of the function
Fu string
Syntax:
Def function name (parameter list):
'document string for function'
Statement block
Example:
def hello(name):
'' this is a function of greeting others
Name name of the binding person
'''
pass
Explain:
The document string is usually used to illustrate the usage of the function
The document string of the function is bound to the doc property of the function object
Functionaldocattribute
__Doc property the document string used to bind the function
Complete syntax for function definition statements:
[@ decorator name 1]
[@ decorator Name2]
…
Def function name ([location parameter], [* tuple parameter], [named key parameter]
, [* * dictionary parameters]):
‘document string’
Statement block