Protocols
In the context of Object Oriented Programming, a protocol is an informal interface that is defined only in the documentation, not in code.
Sequence Protocol
For eg., the sequence protocol in Python entails just the __len__()
and __getitem__()
methods.
Any class Spam
that uses those methods can be used as a sequence.
Whether Spam
is a subclass of this or that is irrelevant;
all that matters is that it provides the necessary methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Because protocols are informal and un-enforced, you can get away with just implementing the part of the protocol that is necessary for your usage.
For example, to support only iteration we just need to implement the __getitem__()
method;
we don't need to implement __len__()
.