4 min read

Python's Data Model and what to keep in mind when I'm coding?

A Concise Overview of Data Model, Special Methods and the Collection API in Python.
Python Programming Language
Photo by Hitesh Choudhary / Unsplash

Fluent Python Book Author and why he wrote the book?

This book is written by “Luciano Ramalho” and published by “O’reily Media”. It is quite popular among Python engineers and Software engineers for serving two very important purposes: Waste less time trying to bend Python to fit patterns from other programming languages and become efficient and productive in the Pythonic ways as fast as possible.

Cover of the Book Fluent Python
Cover of the Book Fluent Python

Why am I writing this and who's it for?

I myself struggled for a short while with Python, but then I started reading this book, to put you in context, I am a Fullstack engineer trying to build a one man SaaS, my research for the best tech stack led me to Python and Django for the backend and since I did not have a good background in Python I made the mistake of thinking it was another Object Oriented programming language.

Eventually I started reading this book and it is a fat book, which takes a good chunk of time if you are in a hurry, so I looked for a summary or a brief resume and I did not find anything that caught my attention, so I decided to do it for anyone out there who is looking for anything similar.

Python version and article series details

  • We will not be getting in to the basics of Python, as this is an advanced book, it is up to you to be up to date with the basics.
  • The latest edition of the book covers Python 3.10 so that’s what we will be tackling too.
  • We will be using examples written by me and examples from the book.
  • This will be a series covering all chapters in the book, I will try to be as brief as possible while relaying the most important informations and avoiding any boilerplate.
  • We will start with the first Chapter which introduces the Python Data Model

The Python Data Model

The Python language provides a set of interfaces that formalizes the building blocks of the language itself, such as sequences, functions, iterators, coroutines, classes, context managers, and so on.

These interfaces form a sort of framework to describe Python and that’s what we call the Python Data Model.

Special Methods

Usually when we code we have to leverage this data model to build our classes, i.e. we implement the methods in these interfaces, these methods are called Special Methods.

We do this when we want our objects to support and interact with fundamental language constructs like:

  • Collections
  • Iteration
  • Operator overloading
  • Asynchronous programming and managed contexts

The offered Special Methods are always written with leading and trailing double underscores, for example : __add__ We implement this method if we want our objects to support the “+” operator.

var = 'hello'
var2 = 'world'

# this is how we should write
print(var + var2)
# helloworld

# this is not how we should write, meant to be called by python interpreter
print(var.__add__(var2))
# helloworld

These special methods are meant to be called by the Python interpreter, and not by us . So we don’t write var.__add__(var2), Instead we write var + var2 after implementing the __add__ method.

If you want to know more about the Python Data Model and the list of Special Methods you can visit here: https://docs.python.org/3/reference/datamodel.html

Collection API

The collection Module in Python provides different types of containers. Here are the most important specializations of the Collection API:

  • 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
UML class diagram with fundamental collection types
UML class diagram with fundamental collection types. Method names in italic are abstract, so they must be implemented by concrete subclasses such as list and dict

All the classes in the diagram are ABCs or Abstract Base Classes and 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 loop, unpacking, and other forms of iteration
  • Sized to support the len built-in function
  • Container to support the in operator

Python does not require concrete classes to actually inherit from any of these ABCs, for example any class that implements __len__ satisfies the Sized interface.

String Representation

A basic requirement for a Python object is to provide usable string representations of itself, one used for debugging and logging, the other for presentation to end users. These special methods are __repr__ and __str__ .

Example:

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)
print(p)
# Point(1, 2)
p.x = 4
print(p)
# Point(4, 2)

Overview of Special Methods

There are more than a 100 special methods, most of them implement arithmetic, bitwise, and comparison operators. Here’s a link to the list of all the special methods: https://docs.python.org/3/reference/datamodel.html#special-method-names

By implementing special methods, your objects can behave like the built-in types, enabling the expressive coding style the community considers Pythonic.

Conclusion

So far we covered The Python Data Model and its Special Methods but it's only the beginning as this can be an extensive subject, it is well documented in Python's Docs so there should be no issues getting into the detail. I will, however,  leave you some extra resources that helped me through my journey.

Further reading