Photo by Artturi Jalli on Unsplash
Python's Data Model, what to keep in mind when I'm coding?
Key notes from the First chapter of the Book Fluent Python
Python is such an easy language to learn but a very difficult one to master, one of the basics of the language is the Data Model.
What is it and when do I use it?
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.
How do I use 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:
Collections
Iteration
Operator overloading
Asynchronous programming and managed contexts
Special Methods
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
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 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 link to the full list.
By implementing Special Methods, your objects can behave like the built-in types, enabling the expressive coding style the community considers Pythonic.
The Collections ABCs
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.
What's next?
For a more detailed explanation regarding this article, you can head to the full article on my Medium Blog where you will find a list of other helpful resources.
Further Reading
The full article on my Blog
The “Data Model” chapter of The Python Language Reference is the canonical source for the subject of this chapter and much of this book.
Python in a Nutshell, 3rd ed. by Alex Martelli, Anna Ravenscroft, and Steve Holden (O’Reilly) has excellent coverage of the data model.
Python Cook‐ book, 3rd ed
The Art of the Metaobject Protocol, explains the concept of a metaobject protocol, of which the Python Data Model is one example