好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

python中关于装饰器级连的示例

装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,下面这篇文章主要给大家介绍了关于python中装饰器级连的使用方法,需要的朋友可以参考借鉴,下面来一起学习学习吧。

#python 3.6 
def star(func): 
  def inner(*args, **kwargs): 
    print("*" * 30) 
    func(*args, **kwargs) 
    print("*" * 30) 
  return inner 
 
def percent(func): 
  def inner(*args, **kwargs): 
    print("%" * 30) 
    func(*args, **kwargs) 
    print("%" * 30) 
  return inner 
 
@star 
@percent 
def printer(msg): 
  print(msg) 
printer("Hello") 
******************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Hello
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
****************************** 

在这个例子里,先 输出星号,也就是先调用第一层装饰器star,接着调用第二层装饰器percent,最后调用函数printer。

总结

以上就是python中关于装饰器级连的示例的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于python中关于装饰器级连的示例的详细内容...

  阅读:43次