python的装饰器是一个函数B 用来装饰另一个函数A(使A具有B的功能,执行A方法同时 也会执行B方法)
这种用法用到 : 内部函数 、 函数传递
没有使用@
def debug(func):
def wrapper():
print 1111
#内部函数返回 入参函数 并执行
return func()
print 2222
# 装饰器函数返回 内部函数 并执行
return wrapper
#函数在返回的时候被执行 先执行装饰器的内部函数 再执行被装饰的函数
def say_hello():
print 3333
print "hello!"
print 4444
say_hello = debug(say_hello)
#新的say_hello方法拥有了 debug的方法 debug函数就是装饰器
say_hello()
#不带括号时,调用的是这个函数本身,是整个函数体,
#带括号时,调用的是函数执行的结果
结果如下
2222
1111
3333
hello!
4444
使用@
def debug(func):
def wrapper():
print 1111
#内部函数返回 入参函数
return func()
print 2222
# 装饰器函数返回 内部函数
return wrapper
@debug
def say_hello():
print 3333
print "hello!"
print 4444
say_hello()
执行的结果是一样 同上所以: @debug <==> say_hello = debug(say_hello)
再来讨论 有参数的装饰器
这里的有参数值的是被装饰的函数,相应的装饰器内部返回函数为了保持一致,也要添加参数
有@
def debug(func):
#say_hello("xxxxxx") 函数传递进入debug
def wrapper(b):
print 4
#内部函数返回 入参函数
return func(b) #say_hello("xxxxxx") wrapper 形参名称和say_hello 不一样也可以
print 3
# 装饰器函数返回 内部函数
return wrapper
@debug
def say_hello(a):
print 1
print a
print 2
say_hello("xxxxxx")
结果如下
3
4
1
xxxxxx
2
分析结果:
1、执行
say_hello("xxxxxx") 带入参的方法 进入装饰器函数debug 顺序执行 先打印 3 在 return wraper时候进入 wraper(a)
打印4 再在 return func(a) 进入执行被装饰 函数 say_hello("xxxxxx") 顺序打印 1 xxxxxx 2 执行完毕
带参数的不用@的时候
def debug(func):
#say_hello("xxxxxx")
def wrapper(b):
print 4
#内部函数返回 入参函数
return func(b)
print 2
print 3
# 装饰器函数返回 内部函数
return wrapper
# @debug
def say_hello(a):
print 1
print a
print 2
say_hello = debug(say_hello)("xxxxxx")
执行结果如上 ,具体执行顺序 琢磨一下
也可以比较下 下面两种不同的声明方法 得到什么样的结果 为什么??
say_hello = debug(say_hello("xxxxxx"))
say_hello = debug(say_hello)("xxxxxx")
另得一下结论:以下三种装饰器的使用是等效的 输出的结果是一样的
@debug
say_hello("xxxxxx")
<==>
debug(say_hello)("xxxxxx")
<==>
say_hello = debug(say_hello)
say_hello("xxxxxx")
推荐这里参考学习 https://www.cnblogs.com/cicaday/p/python-decorator.html