Python's Data Model, what to keep in mind when I'm coding?
Tags: python, webdev, programming, beginners
Published: 2023-04-24 12:33:31

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](https://docs.python.org/3/reference/datamodel.html#special-method-names) 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.

![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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q44b59q0i5jqitvhi1z2.png)

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 [Blog ](https://www.algorystcorner.com/python-data-model/)where you will find a list of other helpful resources.

## Further Reading

- The full article on my [Blog](https://www.algorystcorner.com/python-data-model/)
- The “Data Model” chapter of [The Python Language Reference](https://amzn.to/3HEVa7B) is the canonical source for the subject of this chapter and much of this book.
- [Python in a Nutshell, 3rd ed](https://amzn.to/3HcGcE9). by Alex Martelli, Anna Ravenscroft, and Steve Holden (O’Reilly) has excellent coverage of the data model.
- [Python Essential Reference](https://amzn.to/3wFlesR).
- [Python Cook‐ book, 3rd ed](https://amzn.to/3wFloQZ).
- [The Art of the Metaobject Protocol](https://amzn.to/3JEwH3J), explains the concept of a metaobject protocol, of which the Python Data Model is one example.