Construction of python decorators

! # / usr / bin / to python3 
# - * - Coding: UTF-. 8 - * -
# @time: 2019/9/27 17:04
# @author: v_ctaozhang

Import functools

# without decorator parameter
DEF log (FUNC ):
@ functools.wraps (FUNC)
DEF warpper (* args , ** kwargs):
Print ( . 'Call% S ()'% FUNC the __name__)
return FUNC (* args , ** kwargs)
return warpper

@log
DEF now ():
Print ( " the time now is : 2019 Nian 9 Yue 27 Ri ")

now ()

# decorators with arguments
DEF log (text):
DEF decorator (FUNC):
functools.wraps @ (FUNC)
DEF warpper (* args , ** kwargs):
Print ( '% S% S ():'% (text ., FUNC the __name__))
return FUNC (* args , ** kwargs)
return warpper
decorator return

@log ( " I'm a decorator with arguments ")
DEF nowAndPrint ():
Print ( " the way I was with decorative packaging parameters ")

nowAndPrint ()

# prints out the name of the function called
Print (now. __name__ )
Print (nowAndPrint. __name__)

Guess you like

Origin www.cnblogs.com/cmnz/p/11599424.html