内容简介:将@deco 替换为 myfunc = deco(myfunc) 程序首先调用deco(myfunc),得到的返回结果赋值给了myfunc (注意:在Python中函数名只是个指向函数首地址的函数指针而已) 而deco(myfunc)的返回值就是函数myfunc()的地址 这样其实myfunc 没有变化,也就是说,最后的两次myfunc()函数调用,其实都没有执行到deco()。 有同学就问了,明明打印了deco()函数里面的内容啊,怎么说没有调用到呢。这位同学一看就是没有注意听讲,那一次打印是在@dec
插队插队
def foo(func):
print("start dingdingding..")
def wapper():
print("wo shi GEGE")
func()
return wapper
@foo
def brother():
print("wo shi didi")
brother()
'''
第一步执行:@foo = brother = foo(brother)
2、 foo(func) = foo(brother) func = brother
3、 print("start dingdingding..")
4、 def wapper():
5、 return wapper 也就是说调用def foo(func) 最终返回的是 wapper 那也就是相当于了第一步 @foo = brother = foo(brother) 也就是brother = wapper
6、 brother() 如上 brother加括号 相当于 wapper加括号
7、 执行 def wapper()
8、 print("wo shi GEGE")
9、 func() 在如上我们就知道 func = brother
10、 执行 def brother():
11、 print("wo shi didi")
'''
示例一
# -*- coding:gbk -*-
'''示例1: 使用语法糖@来装饰函数,相当于“myfunc = deco(myfunc)”
但发现新函数只在第一次被调用,且原函数多调用了一次'''
def deco(func):
print("before myfunc() called.")
func()
print(" after myfunc() called.")
return func
@deco
def myfunc():
print(" myfunc() called.")
myfunc()
这里@deco这一句,和myfunc = deco(myfunc)其实是完全等价的,只不过是换了一种写法而已
将@deco 替换为 myfunc = deco(myfunc) 程序首先调用deco(myfunc),得到的返回结果赋值给了myfunc (注意:在 Python 中函数名只是个指向函数首地址的函数指针而已) 而deco(myfunc)的返回值就是函数myfunc()的地址 这样其实myfunc 没有变化,也就是说,最后的两次myfunc()函数调用,其实都没有执行到deco()。 有同学就问了,明明打印了deco()函数里面的内容啊,怎么说没有调用到呢。这位同学一看就是没有注意听讲,那一次打印是在@deco 这一句被执行的。大家亲自动手试一下就会发现” myfunc() called.” 这句打印输出了三次。多的那次就是@deco这里输出的,因为@deco 等价于myfunc = deco(myfunc),这里已经调用了deco()函数了。
第二步 :确保装饰器被调用
怎么解决装饰器没有被调用的问题呢
# -*- coding:gbk -*-
'''示例2: 使用内嵌包装函数来确保每次新函数都被调用,
内嵌包装函数的形参和返回值与原函数相同,装饰函数返回内嵌包装函数对象'''
import time
def deco(func):
def _deco():
print("before myfunc called")
func()
print("after myfunc alled")
return _deco
@deco ##myfunc = deco(myfunc) = _deco
def myfunc():
print("myfunc called at {0}".format(time.strftime('%Y.%m.%d-%H:%M:%S')))
myfunc()
这里其实不需要我解释了,还是按照第一步中的方法做替换就可以了。还是啰嗦几句吧。。 @deco 替换为 myfunc = deco(myfunc) 程序首先调用deco(myfunc),得到的返回结果赋值给了myfunc ,这样myfunc 就变成了指向函数_deco()的指针 以后的myfunc(),其实是调用_deco()
第三步 :对带参数的函数进行装饰
破案过程和第一步、第二步完全一致,不再重复了
# -*- coding:gbk -*-
'''示例5: 对带参数的函数进行装饰,
内嵌包装函数的形参和返回值与原函数相同,装饰函数返回内嵌包装函数对象'''
def deco(func):
def _deco(a, b):
print("before myfunc() called.")
ret = func(a, b)
print(" after myfunc() called. result: %s" % ret)
return ret
return _deco
@deco
def myfunc(a, b):
print(" myfunc(%s,%s) called." % (a, b))
return a + b
myfunc(1, 2)
编写一个decorator,能在函数调用的前后打印出’begin call’和’end call’的日志。
def log(func):
def wrapper(*args,**kw):
print ('begin call')
func(*args,**kw)
print ('end call')
return wrapper
@log
def f():
print ('this is a app!')
f()
输出结果:
begin call this is a app! end call
以上所述就是小编给大家介绍的《Python装饰器》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 装饰器与元数据反射(1)方法装饰器
- 草根学Python(十六) 装饰器(逐步演化成装饰器)
- 一文读懂 JS 装饰器,这是一个会打扮的装饰器
- 装饰器与元数据反射(2)属与类性装饰器
- Python装饰器
- python--装饰器详解
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Object-Oriented Design Heuristics
Arthur J. Riel / Addison-Wesley Professional / 1996-05-10 / USD 64.99
Product Description Here is the first object-oriented development book to provide specific experience-based guidelines to help developers make the right design decisions. This book offers the next ......一起来看看 《Object-Oriented Design Heuristics》 这本书的介绍吧!