jeudi 12 août 2021

Add pure-Python method to a class implemented in an extension?

In a Python library I'm working on a class ConcreteClassC is implemented by an extension. Besides that, all its methods are declared in a pure-Python ClassC(metaclass=abc.ABCMeta). As of now, all methods of ClassC are @abstractmethods, and ConcreteClassC is registered with calling ClassC.register(ConcreteClassC). Users of the library are supposed to use ClassC only. Methods to instantiate objects of ClassC are provided, and under the hood they instantiate ConcreteClassC.

I want to add a new method which is best implemented in pure Python (i.e. not in an extension). Had it been class inheritance, I'd have added this method as a non-abstract method of ClassC. But as of now ConcreteClassC is not a "real" subclass of ClassC, in sense that my new method would not be available for objects which users instantiate.

Is there a pattern to use in such case? As this is only addition of new functionality (not changing existing one), I'd prefer to do no, or minimal, changes to the existing architecture.

Aucun commentaire:

Enregistrer un commentaire