I have a Python system consisting of around 9-10 classes all implementing most of a fat duck-typed interface and used interchangeably by a large collection of modules. I'm trying to refactor the classes into a core, explicit (i.e. ABC) interface and peripheral functionality, following separation of responsibility, but in order to do that I need to be able to tell when the consumer modules are calling methods outside the core interface.
Suppose I have an ABC with abstract methods:
from abc import ABCMeta, abstractmethod
class MyABC:
__metaclass__ = ABCMeta
@abstractmethod
def foo(self):
pass
I also have a class implementing those abstract methods as well as other methods:
class MyClass(MyABC):
def foo(self):
pass
def bar(self):
pass
instance = MyClass()
>>> isinstance(instance, MyABC)
True
How can I ensure that when I pass instance
to a method do_something
it only uses the methods that are part of MyABC
(in this case foo
) and not any other methods (bar
)? In a static-typed language (e.g. C++) I could pass do_something
a pointer of the ABC type; is there some sort of wrapper available in Python that will restrict method access similarly?