python装饰器
参考资料:1. Python装饰器学习 http://blog.iyunv.com/thy38/article/details/4471421
2. Python装饰器与面向切面编程 http://www.iyunv.com/huxi/archive/2011/03/01/1967600.html
3. Python装饰器的理解 http://apps.hi.baidu.com/share/detail/17572338
装饰器其实也就是一个函数,一个用来包装函数的函数,返回一个修改之后的函数对象,将其重新赋值原来的标识符,并永久丧失对原始函数对象的访问,要注意的是装饰函数必须返回被装饰的函数,否则会出错!
1.无参数的装饰器
def deco(func):
print 'This is func'
return func#一定要有返回值
@deco
def b(a):
print 'This is b,more:'+str(a)
if __name__=='__main__':
b('hello,kity')
运行结果为:
This is func
This is b,more:hello,kity
其中函数b式被装饰的函数,函数deco式装饰函数。由于添加了装饰器,所以调用之前等价于有赋值语句:
b = deco(b)
所以调用的时候顺序依次为:调用deco函数,参数为b,在函数deco中先打印出'This is func'语句,之后返回函数引用b,然后调用被修饰的函数b,参数为'hello,kity'。
2.有参数的装饰器
import time
def deco(func):
def _inf(a):
t = time.clock()
func(a)
print 'used:',time.clock()-t
#return func
return _inf
@deco
def b(a):
print 'This is b,more:'+str(a)
if __name__=='__main__':
b('hello,kity')
运行结果为:
This is b,more:hello,kity
used: 2.19307966712e-05
因为要在修饰函数内调用函数func,而且有参数,但是如果不在装饰器内定义新的函数,这个参数是无法获取的,像上面的情况,虽然也有参数,但是这个是在修饰器是在外面使用的参数(由修饰器返回函数引用,再通过该应用来调用参数,所以不属于修饰器内),所以需要定义新的函数。
调用顺序为:调用b('hello,kity')时,先调用装饰函数deco,由于装饰函数内只返回其内建的新函数,所以该函数直接返回内建函数的引用,然后通过该引用调用参数,此时就会调用deco的内建函数_inf,参数为'hello,kity',所以就会计时,然后打印出来。
3.内置的装饰函数
内置的装饰器有三个,分别是staticmethod、classmethod和property,作用分别是把类中定义的实例方法变成静态方法、类方法和类属性。
import time
def deco(func):
def _inf(a):
t = time.clock()
func(a)
print 'used:',time.clock()-t
#return func
return _inf
@deco
def b(a):
print 'This is b,more:'+str(a)
class Point(object):
def __init__(self,name):
self.name = name
print '__init__','set name to:',self._name
@staticmethod
def size(self):
print 'sorry,ihave no size!'
@classmethod
def cf(self):
return Point('')
@property
def name(self):
return self.name
@name.setter
def name(self,name):
self._name = name
if __name__=='__main__':
b('hello,kity')
p = Point('p1')
Point.cf()
p.cf()
页:
[1]