Decorators on abstract methods

元气小坏坏 提交于 2021-02-07 04:44:05

问题


In python, is there a way to make a decorator on an abstract method carry through to the derived implementation(s)?

For example, in

import abc

class Foo(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    @some_decorator
    def my_method(self, x):
        pass

class SubFoo(Foo):
    def my_method(self, x):
        print x

SubFoo's my_method won't get decorated with some_decorator as far as I can tell. Is there some way I can make this happen without having to individually decorate each derived class of Foo?


回答1:


I would code it as two different methods just like in standard method factory pattern description.

https://www.oodesign.com/factory-method-pattern.html

class Foo(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    @some_decorator
    def my_method(self, x):
        self.child_method()

class SubFoo(Foo):
    def child_method(self, x):
        print x



回答2:


This is, of course, possible. There is very little that can't be done in Python haha! I'll leave whether it's a good idea up to you...

class MyClass:
    def myfunc():
        raise NotImplemented()

    def __getattribute__(self, name):
        if name == "myfunc":
            func = getattr(type(self), "myfunc")
            return mydecorator(func)
        return object.__getattribute__(self, name)

(Not tested for syntax yet, but should give you the idea)




回答3:


My solution would be extending the superclass' method without overriding it.

import abc

class Foo(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    @some_decorator
    def my_method(self, x):
        pass

class SubFoo(Foo):
    def my_method(self, x):
        super().my_method(x)  #delegating the call to the superclass
        print x



回答4:


As far as I know, this is not possible and not a good strategy in Python. Here's more explanation.

According to the abc documentation:

When abstractmethod() is applied in combination with other method descriptors, it should be applied as the innermost decorator, as shown in the following usage examples: ...

In other words, we could write your class like this (Python 3 style):

from abc import ABCMeta, abstractmethod

class AbstractClass(metclass=ABCMeta):

    @property
    @abstactmethod
    def info(self):
        pass

But then what? If you derive from AbstractClass and try to override the info property without specifying the @property decorator, that would create a great deal of confusion. Remember that properties (and it's only an example) usually use the same name for their class method, for concision's sake:

class Concrete(AbstractMethod):

    @property
    def info(self):
        return

    @info.setter
    def info(self, new_info):
        new_info

In this context, if you didn't repeat the @property and @info.setter decorators, that would create confusion. In Python terms, that won't work either, properties being placed on the class itself, not on the instance. In other words, I guess it could be done, but in the end, it would create confusing code that's not nearly as easy to read as repeating a few decorator lines, in my opinion.



来源:https://stackoverflow.com/questions/19335436/decorators-on-abstract-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!