Decorators on abstract methods Decorators on abstract methods python python

Decorators on abstract methods


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


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)


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

import abcclass Foo(object):    __metaclass__ = abc.ABCMeta    @abc.abstractmethod    @some_decorator    def my_method(self, x):        passclass SubFoo(Foo):    def my_method(self, x):        super().my_method(x)  #delegating the call to the superclass        print x