Say I have a class like the following:
from functools import wrap
import abc
class Test:
def hello (self):
print("hello world!")
def decorator (f):
@wraps(f)
def wrapped (inst, *args, **kwargs):
inst.hello()
return f(inst, *args, **kwargs)
return wrapped
@abc.abstractmethod
# @decorator ???
def fn (self):
return
Is there any way of ensuring that all implementations of the abstract method, fn(), will be decorated with decorator()? Either by enforcing that the implementation explicitly includes the decoration, or by decorating all implementations automatically.
There is not. ABCs can only dictate that certain attributes are present, either as methods or properties. They are not equipped to dictate that a decorator is used.
ABCs specify the interface, not the implementation; a decorator is an implementation detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With