时间:2020-10-11来源:www.pcxitongcheng.com作者:电脑系统城
无参修饰 ,无参数时不需要调用
?1 2 3 4 5 |
def log1(func): func() @log1 def test(): print ( 'test:' ) |
有参修饰
?1 2 3 4 5 6 7 8 |
def log2(func): def inner( * args, * * kwargs): func( * args, * * kwargs) return inner @log2 def test(num): print ( 'testlog2:' ,num,test.__name__) test( 20 ) #相当于log(test(20)) |
@wraps可以保证装饰器修饰的函数的name的值保持不变
不带参数的装饰器
?1 2 3 4 5 6 7 8 9 |
def log3(func): @wraps (func) def inner( * args, * * kwargs,): func( * args, * * kwargs) return inner @log3 def test(num): print ( 'testlog3:' ,num,test.__name__) test( 30 ) #相当于log(test(30)) |
带参数的装饰器
?1 2 3 4 5 6 7 8 9 10 11 12 13 |
def log4(level): def log(func): @wraps (func) def inner( * args, * * kwargs,): if level = = "warn" : print ( "%s is running" % func.__name__) func( * args, * * kwargs) return inner return log @log4 (level = "warn" ) def test(num): print ( 'testlog4:' , num, test.__name__) test( 40 ) |
实现带参数和不带参数的装饰器自适应
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
def log(arg): if callable (arg): # 判断参入的参数是否是函数,不带参数的装饰器调用这个分支 def log3(func): @wraps (func) def inner( * args, * * kwargs, ): func( * args, * * kwargs) return inner return log3 else : def log4(func): @wraps (func) def inner( * args, * * kwargs,): if arg = = "warn" : print ( "%s is running" % func.__name__) func( * args, * * kwargs) return inner return log4 @log (arg = None ) def test(num): print ( 'testlog:' , num, test.__name__) test( 0 ) |
返回入参出参
?1 2 3 4 5 6 7 8 9 10 11 12 |
def log5(func): def inner( * args, * * kwargs): print ( '入参:' ,func.__name__, args, kwargs) res = func( * args, * * kwargs) print ( '出参:' ,func.__name__, res) return res return inner @log5 def test(num): print ( 'testlog5:' , num, test.__name__) return num print (test( 50 )) |
类装饰器
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Loging: def __init__( self ,level): self .level = level def __call__( self ,func): @wraps (func) def inner( * args, * * kwargs): if self .level = = "warn" : self .notify(func) func( * args, * * kwargs) return inner def notify( self ,func): print ( "%s is running" % func.__name__) @Loging (level = "warn" ) def test(num): print ( 'testLoging:' , num, test.__name__) test( 0 ) |
以上就是python 装饰器的使用示例的详细内容,
2023-03-17
python flask项目打包成docker镜像发布的过程2023-03-17
python调试模块ipdb详解2023-03-17
python使用openai生成图像的超详细教程python cron定时任务触发接口自动化巡检 apscheduler报错:Run time of job …… next run at: ……)” was missed by misfire_grace_time参数 找到任务超时的根本原因...
2023-03-15