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

标签 python class python-3.x decorator

我想对给定类中的每个方法应用相同的装饰器,除了那些以 __ 开头和结尾的方法。

在我看来,使用类装饰器应该是可行的。有什么需要注意的陷阱吗?

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

  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

以下是我遇到的问题:

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

最佳答案

我认为这最好用元类来完成,以便同时处理运行时和子类方法修饰。我没有看到使用类装饰器自动处理子类的优雅方式。

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

及其使用示例(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>

关于python - 自动装饰类中的每个实例方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10067262/

相关文章:

python - Matplotlib xtick 给出 "The truth value of an array with more than one element is ambiguous"

python - pygame 中出现游戏结束屏幕后重新启动贪吃蛇游戏

python - 属性错误: type object 'projectile' has no attribute 'dir'

python - 创建图像查看器并允许通过 python 缩放、拖动和画线的最佳方法

C++ 未处理的异常。 0xC0000005 : Access violation reading location 0xccccccd0

class - 从 Lua 的类中获取表

python-3.x - 如何使用 python 3.8 获取 AWS S3 对象位置/URL?

python - 我们应该把所有必需的包和它们的依赖包放在 requirements.txt 上还是只放需要的包

python - python的xml.etree.ElementTree支持DTD吗?

python - 将一组函数作为参数传递给 Python 中的另一个函数