自动装饰类中的每个实例方法

max*_*max 10 python class decorator python-3.x

我想将相同的装饰器应用于给定类中的每个方法,而不是那些以开始和结束方式开始的方法__.

在我看来,使用类装饰器应该是可行的.是否有任何陷阱需要注意?

理想情况下,我也希望能够:

  1. 通过使用特殊装饰器标记它们来禁用某些方法的此机制
  2. 也为子类启用此机制
  3. 即使对于在运行时添加到此类的方法,也启用此机制

[注意:我使用的是Python 3.2,所以如果这依赖于最近添加的功能,我会很好.

这是我的尝试:

_methods_to_skip = {}

def apply(decorator):
  def apply_decorator(cls):
    for method_name, method in get_all_instance_methods(cls):
      if (cls, method) in _methods_to_skip:
        continue
      if method_name[:2] == `__` and method_name[-2:] == `__`:
        continue
      cls.method_name = decorator(method)
  return apply_decorator

def dont_decorate(method):
  _methods_to_skip.add((get_class_from_method(method), method))
  return method
Run Code Online (Sandbox Code Playgroud)

以下是我遇到的问题:

  • 如何实现get_all_instance_methods功能
  • 不确定我的cls.method_name = decorator(method)线是否正确
  • 如何对运行时添加到类中的任何方法执行相同操作
  • 如何将其应用于子类
  • 如何实施 get_class_from_method

agf*_*agf 15

我认为使用元类更好地完成,以便处理运行时和子类方法的装饰.我没有看到使用类装饰器自动处理子类的优雅方法.

from types import FunctionType

# check if an object should be decorated
def do_decorate(attr, value):
    return ('__' not in attr and
            isinstance(value, FunctionType) and
            getattr(value, 'decorate', True))

# decorate all instance methods (unless excluded) with the same decorator
def decorate_all(decorator):
    class DecorateAll(type):
        def __new__(cls, name, bases, dct):
            for attr, value in dct.iteritems():
                if do_decorate(attr, value):
                    dct[attr] = decorator(value)
            return super(DecorateAll, cls).__new__(cls, name, bases, dct)
        def __setattr__(self, attr, value):
            if do_decorate(attr, value):
                value = decorator(value)
            super(DecorateAll, self).__setattr__(attr, value)
    return DecorateAll

# decorator to exclude methods
def dont_decorate(f):
    f.decorate = False
    return f
Run Code Online (Sandbox Code Playgroud)

它的一个使用示例(Python 2,但对Python 3进行了简单的修改):

def printer(f):
    print f
    return f

class Foo(object):
    __metaclass__ = decorate_all(printer)
    def bar(self):
        pass
    @dont_decorate
    def baz(self):
        pass
    @classmethod
    def test(self):
        pass
# prints
# <function bar at 0x04EB59B0>

class AnotherName(Foo):
    def blah(self):
        pass
# prints
# <function blah at 0x04EB5930>

Foo.qux = lambda: 1
# prints
# <function <lambda> at 0x04EB57F0>
Run Code Online (Sandbox Code Playgroud)