I would like to systematically wrap some overriden method of a base class.
I am using ABC
for the base class. I tried to wrap the @abstractmethod
, putting the annotation before or after, but it doesn’t work. As I understand it, the the whole wrapped method is overriden.
from functools import wraps
from abc import ABC, abstractmethod
def print_before(func):
@wraps(func)
def out(*args, **kwargs):
print('Hello')
return func(*args, **kwargs)
return out
class Base(ABC):
@print_before
@abstractmethod
def test(self):
pass
class Extend(Base):
def test(self):
print('World')
Here is what happens when we test:
Extend().test()
Result:
World
Desired:
Hello
World
I guess I’m not using the right method to get such behavior. What would be a nice pythonic way to run some code before and after an overriden method?