Collection protocols

In Python, a protocol is a group of operations or methods that a type must support if it is to implement that protocol. Protocols needn't be defined in the source code as separate interfaces or base classes as they would in a nominally typed language such as C# or Java. It's sufficient to simply have an object provide functioning implementations of those operations.

We can organize the different collections we have encountered in Python according to which protocols they support:

Protocol Implementing collections
Container str, list, dict, range, tuple, set, bytes
Sized str, list, dict, range, tuple, set, bytes
Iterable str, list, dict, range, tuple, set, bytes
Sequence str, list, tuple, range, bytes
Mutable Sequence list
Mutable Set set
Mutable Mapping dict

Support for a protocol demands specific behavior from a type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset