繁体   English   中英

在Python中装饰实例方法

[英]Decorating Instance Methods in Python

这是我要做的事情的要点。 我有一个对象列表,我知道他们有一个实例方法,如下所示:

def render(self, name, value, attrs)
   # Renders a widget...

我想(essentialy)在运行时装饰这些函数,因为我正在迭代对象列表。 这样他们的渲染函数就变成了:

def render(self, name, value, attrs)
   self.attrs=attrs
   # Renders a widget...

两个警告:

  1. 渲染函数是django的一部分。 我不能把装饰器放在他们的库中(我可以,但是我必须维护和迁移这个改变)。
  2. 这是一个实例方法。

这里有一个例子: http//wiki.python.org/moin/PythonDecoratorLibrary

演示如何向类添加新的实例方法。 这里的区别是我想记住那个attrs参数后我想要落到原来的方法。

def decorate_method(f):
  def wrapper(self, name, value, attrs):
    self.attrs = attrs
    return f(self, name, value, attrs)
  return wrapper

def decorate_class(c):
  for n in dir(c):
    f = getattr(c, n)
    if hasattr(f, 'im_func'):
      setattr(c, n, decorate_method(f.im_func))

您可能需要一些其他测试来跳过具有不同签名的方法,但除此之外, decorate_class(whatever)应该在任何给定的类上执行您想要的任何whatever

“经典”方式是子类。 这样你就不必乱搞别人的课了。

class someclass(object):
    def render(self, name, value, attrs):
        print hasattr(self, 'attrs')

class my_render(object):
    def render(self, name, value, attrs):
        self.attrs = attrs # kind of decorating the function here
        return super(my_render, self).render(name, value, attrs)

class my_class(my_render, someclass): 
    pass    

someclass().render(1,2,3) # -> False
my_class().render(1,2,3) # -> True

MI的原因是所有类都可以从my_render继承。 我喜欢mixin概念;-)

class my_otherclass(my_render, someotherclass): pass
class my_thirdclass(my_render, thirdclass): pass

# or less explicit
classlist = [ someclass, someotherclass ]
newclasses = [ type('my_'+cls.__name__, (my_render,cls), {}) for cls in classlist ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM