Python is such an easy language to learn but it is a very difficult one to master. One of the basics of the language is the Data Model.
The Data Model is a set of interfaces that formalizes the building blocks of the Python language itself, such as sequences, functions, iterators, coroutines, and so on. It is used all the time as you cannot write relevant Pythonic code without it.
The short answer is we implement the methods in these interfaces.
When we want our objects to support and interact with fundamental language constructs like:
You probably came across this method __init__
and others written with leading and trailing double underscores, this is a Special Method serving as a Constructor for your Object.
Special Methods are meant to be called by the Python Interpreter and not by us, we just implement them if needed, and we use the syntactic sugar offered to us like with this __add__
that gets called by the “+” operator:
var = 'hello'
var2 = ' world'
# this is how we should write
print(var + var2) # hello world
# this is not how we should write, meant to be called by Python Interpreter
print(var.__add__(var2)) # hello world
A Python object should also provide usable string representations of itself, one used for debugging and logging, and another for presentation to end users. These Special Methods are __repr__
and __str__
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"Point({self.x}, {self.y})"
def __str__(self):
# Default implementation, to change depending on user needs
return self.__repr__()
>>> p = Point(1, 2)
>>> p
Point(1, 2)
>>> p.x = 4
>>> p
Point(4, 2)
There are more than 100 Special Methods, most of which implement Arithmetic, Bitwise, and Comparison Operators. Here’s a
By implementing Special Methods, your objects can behave like the built-in types, enabling the expressive coding style the community considers Pythonic.
The Collection API is an Integral Module of the Python Data Model, this API offers us interfaces for these types:
Sequence, formalizing the interface of built-ins like list and str
Mapping, implemented by dict, collections.defaultdict, etc.
Set, the interface of the set, and frozenset built-in types.
All the classes in the diagram are ABCs or Abstract Base Classes. Each of the top ABCs has a single special method.
The Collection ABC unifies the three essential interfaces that every collection should implement:
Iterable to support for, unpacking, and other forms of iteration
Sized to support the len built-in function
Container to support the in operator
Keep in mind that it is not required to inherit from any of these ABCs, for example, any class that implements __len__
satisfies the Sized interface.
Also published (in full) here.