[Python] Decorators on abstract methods

Discussão em 'Python' iniciado por Stack, Agosto 1, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página