发布时间:2022-04-19 09:56:13 人气:224 作者:多测师
1. 语法糖
上面这段代码看起来似乎已经不能再精简了,Python于是提供了一个语法糖来降低字符输入量。
import time
def timeit(func):
def wrapper():
start = time.clock()
func()
end =time.clock()
print 'used:', end - start
return wrapper
@timeit
def foo():
print 'in foo()'
foo()
重点关注第11行的@timeit,在定义上加上这一行与另外写foo = timeit(foo)完全等价,千万不要以为@有另外的魔力。除了字符输入少了一些,还有一个额外的好处:这样看上去更有装饰器的感觉。
2. 内置的装饰器
内置的装饰器有三个,分别是staticmethod、classmethod和property,作用分别是把类中定义的实例方法变成静态方法、类方法和类属性。由于模块里可以定义函数,所以静态方法和类方法的用处并不是太多,除非你想要完全的面向对象编程。而属性也不是不可或缺的,Java没有属性也一样活得很滋润。从我个人的Python经验来看,我没有使用过property,使用staticmethod和classmethod的频率也非常低。
class Rabbit(object):
def __init__(self, name):
self._name = name
@staticmethod
def newRabbit(name):
return Rabbit(name)
@classmethod
def newRabbit2(cls):
return Rabbit('')
@property
def name(self):
return self._name
这里定义的属性是一个只读属性,如果需要可写,则需要再定义一个setter:
@name.setter
def name(self, name):
self._name = name
3. functools模块
functools模块提供了两个装饰器。这个模块是Python 2.5后新增的,一般来说大家用的应该都高于这个版本。但我平时的工作环境是2.4 T-T
3.1. wraps(wrapped[, assigned][, updated]):
这是一个很有用的装饰器。看过前一篇反射的朋友应该知道,函数是有几个特殊属性比如函数名,在被装饰后,上例中的函数名foo会变成包装函数的名字wrapper,如果你希望使用反射,可能会导致意外的结果。这个装饰器可以解决这个问题,它能将装饰过的函数的特殊属性保留。
import time
import functools
def timeit(func):
@functools.wraps(func)
def wrapper():
start = time.clock()
func()
end =time.clock()
print 'used:', end - start
return wrapper
@timeit
def foo():
print 'in foo()'
foo()
print foo.__name__
首先注意第5行,如果注释这一行,foo.__name__将是'wrapper'。另外相信你也注意到了,这个装饰器竟然带有一个参数。实际上,他还有另外两个可选的参数,assigned中的属性名将使用赋值的方式替换,而updated中的属性名将使用update的方式合并,你可以通过查看functools的源代码获得它们的默认值。对于这个装饰器,相当于wrapper = functools.wraps(func)(wrapper)。
3.2. total_ordering(cls):
这个装饰器在特定的场合有一定用处,但是它是在Python 2.7后新增的。它的作用是为实现了至少__lt__、__le__、__gt__、__ge__其中一个的类加上其他的比较方法,这是一个类装饰器。如果觉得不好理解,不妨仔细看看这个装饰器的源代码:
53 def total_ordering(cls):
54 """Class decorator that fills in missing ordering methods"""
55 convert = {
56 '__lt__': [('__gt__', lambda self, other: other < self),
57 ('__le__', lambda self, other: not other < self),
58 ('__ge__', lambda self, other: not self < other)],
59 '__le__': [('__ge__', lambda self, other: other <= self),
60 ('__lt__', lambda self, other: not other <= self),
61 ('__gt__', lambda self, other: not self <= other)],
62 '__gt__': [('__lt__', lambda self, other: other > self),
63 ('__ge__', lambda self, other: not other > self),
64 ('__le__', lambda self, other: not self > other)],
65 '__ge__': [('__le__', lambda self, other: other >= self),
66 ('__gt__', lambda self, other: not other >= self),
67 ('__lt__', lambda self, other: not self >= other)]
68 }
69 roots = set(dir(cls)) & set(convert)
70 if not roots:
71 raise ValueError('must define at least one ordering operation: < > <= >=')
72 root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__
73 for opname, opfunc in convert[root]:
74 if opname not in roots:
75 opfunc.__name__ = opname
76 opfunc.__doc__ = getattr(int, opname).__doc__
77 setattr(cls, opname, opfunc)
78 return cls
以上内容为大家介绍了Python的额外支持,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/