In a way, a Python’s data model can be viewed as a framework. It formalizes the interfaces of the building blocks of the language itself.
The Python interpreter invokes special methods to perform basic object operations, often triggered by special syntax. That provides a consistency in usage of objects from standard library, as well as custom ones.
Example
__len__
special method will be called by thelen()
function__getitem__
- used to read items from the sequence.
Benefits
- Users don’t have to memorize arbitrary methods names for standard operations (“How to get the number of items? Is it
.size()
,.length()
, or what?”) - It’s easier to benefit from the rich Python standard library and avoid reinventing the wheel, like the
random.choice
function or the fact that with__getitem__
method we can read specific item in the sequence, slice or iterate it.