Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining abc.abstractmethod with other decorators

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.

like image 399
Pig Avatar asked Oct 24 '25 23:10

Pig


1 Answers

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.

like image 165
Martijn Pieters Avatar answered Oct 27 '25 14:10

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!