深入理解Python中装饰器的用法
|
因为函数或类都是对象,它们也能被四处传递。它们又是可变对象,可以被更改。在函数或类对象创建后但绑定到名字前更改之的行为为装饰(decorator)。 “装饰器”后隐藏了两种意思――一是函数起了装饰作用,例如,执行真正的工作,另一个是依附于装饰器语法的表达式,例如,at符号和装饰函数的名称。 函数可以通过函数装饰器语法装饰: @decorator # ② def function(): # ① pass函数以标准方式定义。① 以@做为定义为装饰器函数前缀的表达式②。在 @ 后的部分必须是简单的表达式,通常只是函数或类的名字。这一部分先求值,在下面的定义的函数准备好后,装饰器被新定义的函数对象作为单个参数调用。装饰器返回的值附着到被装饰的函数名。 装饰器可以应用到函数和类上。对类语义很明晰――类定义被当作参数来调用装饰器,无论返回什么都赋给被装饰的名字。 在装饰器语法实现前(PEP 318),通过将函数和类对象赋给临时变量然后显式调用装饰器然后将返回值赋给函数名,可以完成同样的事。这似乎要打更多的字,也确实装饰器函数名用了两次同时临时变量要用至少三次,很容易出错。以上实例相当于: def function(): # ① pass function = decorator(function) # ②装饰器可以堆栈(stacked)――应用的顺序是从底到上或从里到外。就是说最初的函数被当作第一次参数器的参数,无论返回什么都被作为第二个装饰器的参数……无论最后一个装饰器返回什么都被依附到最初函数的名下。 装饰器语法因其可读性被选择。因为装饰器在函数头部前被指定,显然不是函数体的一部分,它只能对整个函数起作用。以@为前缀的表达式又让它明显到不容忽视(根据PEP叫在您脸上……:))。当多个装饰器被应用时,每个放在不同的行非常易于阅读。 代替和调整原始对象 实现类和函数装饰器 让我们比较函数和类方法。装饰器表达式(@后部分)可以只是名字。只有名字的方法很好(打字少,看起来整洁等),但是只有当无需用参数定制装饰器时才可能。被写作函数的装饰器可以用以下两种方式:
>>> def simple_decorator(function):
... print "doing decoration"
... return function
>>> @simple_decorator
... def function():
... print "inside function"
doing decoration
>>> function()
inside function
>>> def decorator_with_arguments(arg):
... print "defining the decorator"
... def _decorator(function):
... # in this inner function,arg is available too
... print "doing decoration,",arg
... return function
... return _decorator
>>> @decorator_with_arguments("abc")
... def function():
... print "inside function"
defining the decorator
doing decoration,abc
>>> function()
inside function
这两个装饰器属于返回被装饰函数的类别。如果它们想返回新的函数,需要额外的嵌套,最糟的情况下,需要三层嵌套。
>>> def replacing_decorator_with_args(arg):
... print "defining the decorator"
... def _decorator(function):
... # in this inner function,arg
... def _wrapper(*args,**kwargs):
... print "inside wrapper,args,kwargs
... return function(*args,**kwargs)
... return _wrapper
... return _decorator
>>> @replacing_decorator_with_args("abc")
... def function(*args,**kwargs):
... print "inside function,kwargs
... return 14
defining the decorator
doing decoration,abc
>>> function(11,12)
inside wrapper,(11,12) {}
inside function,12) {}
14
_wrapper函数被定义为接受所有位置和关键字参数。通常我们不知道哪些参数被装饰函数会接受,所以wrapper将所有东西都创递给被装饰函数。一个不幸的结果就是显式参数很迷惑人。 相比定义为函数的装饰器,定义为类的复杂装饰器更简单。当对象被创建,__init__方法仅仅允许返回None,创建的对象类型不能更改。这意味着当装饰器被定义为类时,使用无参数的形式没什么意义:最终被装饰的对象只是装饰类的一个实例而已,被构建器(constructor)调用返回,并不非常有用。讨论在装饰表达式中给出参数的基于类的装饰器,__init__方法被用来构建装饰器。
>>> class decorator_class(object):
... def __init__(self,arg):
... # this method is called in the decorator expression
... print "in decorator init,arg
... self.arg = arg
... def __call__(self,function):
... # this method is called to do the job
... print "in decorator call,self.arg
... return function
>>> deco_instance = decorator_class('foo')
in decorator init,foo
>>> @deco_instance
... def function(*args,**kwargs):
... print "in function,kwargs
in decorator call,foo
>>> function()
in function,() {}
相对于正常规则(PEP 8)由类写成的装饰器表现得更像函数,因此它们的名字以小写字母开始。 事实上,创建一个仅返回被装饰函数的新类没什么意义。对象应该有状态,这种装饰器在装饰器返回新对象时更有用。
>>> class replacing_decorator_class(object):
... def __init__(self,self.arg
... self.function = function
... return self._wrapper
... def _wrapper(self,*args,**kwargs):
... print "in the wrapper,kwargs
... return self.function(*args,**kwargs)
>>> deco_instance = replacing_decorator_class('foo')
in decorator init,foo
>>> function(11,12)
in the wrapper,12) {}
in function,12) {}
像这样的装饰器可以做任何事,因为它能改变被装饰函数对象和参数,调用被装饰函数或不调用,最后改变返回值。 复制原始函数的文档字符串和其它属性
>>> import functools
>>> def better_replacing_decorator_with_args(arg):
... print "defining the decorator"
... def _decorator(function):
... print "doing decoration,**kwargs)
... return functools.update_wrapper(_wrapper,function)
... return _decorator
>>> @better_replacing_decorator_with_args("abc")
... def function():
... "extensive documentation"
... print "inside function"
... return 14
defining the decorator
doing decoration,abc
>>> function
<function function at 0x...>
>>> print function.__doc__
extensive documentation
一件重要的东西是从可迁移属性列表中所缺少的:参数列表。参数的默认值可以通过__defaults__、__kwdefaults__属性更改,但是不幸的是参数列表本身不能被设置为属性。这意味着help(function)将显式无用的参数列表,使使用者迷惑不已。一个解决此问题有效但是丑陋的方式是使用eval动态创建wrapper。可以使用外部external模块自动实现。它提供了对decorator装饰器的支持,该装饰器接受wrapper并将之转换成保留函数签名的装饰器。 综上,装饰器应该总是使用functools.update_wrapper或者其它方式赋值函数属性。 标准库中的示例 classmethod让一个方法变成“类方法”,即它能够无需创建实例调用。当一个常规方法被调用时,解释器插入实例对象作为第一个参数self。当类方法被调用时,类本身被给做第一个参数,一般叫cls。
class Array(object):
def __init__(self,data):
self.data = data
@classmethod
def fromfile(cls,file):
data = numpy.load(file)
return cls(data)
这比用一大堆标记的__init__简单多了。 >>> class A(object): ... @property ... def a(self): ... "an important attribute" ... return "a value" >>> A.a <property object at 0x...> >>> A().a 'a value' (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 从“今天”还是“昨天”到Python的时间,最好的方法是什么?
- python2和python3应该学哪个(python3.6与python3.7的选择)
- python – Pandas.dataframe.query() – 获取非空行(Pandas
- Python使用arrow库优雅地处理时间数据详解
- python – 有没有办法在argparse中添加已创建的解析器作为s
- python – 两个numpy数组中所有行的组合
- 一个python自定义的加密解密函数
- 在Django-South中,Pesky“Table’my_table’已经存在”
- Python操作SQLite简明教程
- 将ipython输出自动化为pdf
