【问题标题】:Using __getattr__ for implementing the decorator design pattern [duplicate]使用 __getattr__ 实现装饰器设计模式 [重复]
【发布时间】:2013-06-25 08:51:07
【问题描述】:

假设你有class Foo,它有4个方法和class FooDecorator。在这4个方法中,FooDecorator只需要装饰一个方法。

从 pythonic/OO 设计的角度来看,使用 __getattr__ 将请求转发到装饰实例是否可以?这基本上会删除 deocrator 类中的大量样板代码。

例子:

class Foo(object):

    def method_1(self):
        return some, stuff

    def method_2(self):
        return some, stuff

    def method_3(self):
        return some, stuff

    def method_4(self):
        return some, stuff

class FooClassicDecorator(object):
    def __init__(self, foo_instance):
        self._foo_instance = foo_instance

    def method_1(self):
        returned = self._foo_instance.method_1()
        return decorate_result(returned)

    def method_2(self):
        return self._foo_instance.method_2()

    def method_3(self):
        return self._foo_instance.method_3()

    def method_4(self):
        return self._foo_instance.method_4()

class FooGetattrDecorator(ojbect):
    def __init__(self, foo_instance):
        self._foo_instance = foo_instance

    def __getattr__(self, attr_name):
        return getattr(self._foo_instance, attr_name)

    def method_1(self):
        returned = self._foo_instance.method_1()
        return decorate_result(returned)

你觉得哪个更优雅? FooClassicDecoratorFooGetattrDecorator ?

【问题讨论】:

  • 我想我也不会使用。我只是继承Foo 并覆盖一个方法。
  • @Aya 是的...但是装饰器的重点是您应该能够装饰已经存在的实例。以 wikipedia 中的示例为例,您应该可以做 Sprinkles(Milk(Whip(SimpleCoffe()))) 或通过 Milk(Milk(SimpleCoffe())) 来一杯拿铁咖啡。组合的数量或应用装饰器的顺序可能非常庞大。
  • 那你为什么不直接使用 Python decorator 呢?
  • 通过。我真的无法想象一个具体的例子来说明我什么时候想在 Python 中使用这种模式。由于 Python 的动态特性,许多其他语言的设计模式最终成为 Python 中的反模式。
  • @Aya 我同意......但不适用于这种特殊情况。模式在静态语言中有用的相同用例在动态语言中也很有用。

标签: python design-patterns decorator


【解决方案1】:

我发现了另一个非常相似的问题。投票和接受的最高答案似乎与我的 FooGetattrDecorator 非常相似

Implementing the decorator pattern in Python

关闭它并标记为重复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 2020-02-29
    • 2017-08-01
    相关资源
    最近更新 更多