Given methods:
def foo(bar):
bar.buzz()
bar.blep()
def bla(bee):
bee.buzz()
Now, we know that bar
has to have buzz
and blep
methods. But the clients of the code does not know that unless they open the implementation and read through it (which can be easy in this example but can be tricky in the real world).
One solution is to define an ABC
, like so:
def BuzzBlepable(ABC):
@abstractmethod
def buzz():
pass
@abstractmethod
def blep():
pass
Now we can have the code:
def foo(bar: BuzzBlepable):
bar.buzz()
bar.blep()
def bla(bee: BuzzBlepable):
bee.buzz()
The problem with this is that bla
method doesn't really need the bee
parameter to implement blep
method, but we will be forcing the client to do so anyway. This begs a question, is there any good practice to tell the client which interface the parameter has to conform to? Like, auto-generated method documentation or something? Has anyone any ideas?