Python装饰器代码示范



贴上代码如下:

注:*代表任意位置参数

       **代表任意关键字参数

>>> def decorator(func):
 def a(*args,**kw):
  print('装饰的内容就在这儿')
  return func(*args,**kw)
 return a

>>> @decorator
def b():
 print('原生态内容就在这儿')

 
>>> b()
装饰的内容就在这儿
原生态内容就在这儿
>>>

>>> c=decorator(b)
>>> c()
装饰的内容就在这儿
装饰的内容就在这儿
原生态内容就在这儿
>>>

>>> d=decorator(c)
>>> d()
装饰的内容就在这儿
装饰的内容就在这儿
装饰的内容就在这儿
原生态内容就在这儿
>>>



扫描二维码关注公众号,回复: 3051112 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_38835878/article/details/76982844