What You Need To Know About Python’s Data Model

0
253

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.

What is a data model 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 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 ArithmeticBitwise, 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 API

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 dictcollections.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. Picture from the Book Fluent Python

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 forunpacking, 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.


Further Reading

By: Nadim Jendoubi
Published at Hackernoon

Source: Cyberpogo



For enquiries, product placements, sponsorships, and collaborations, connect with us at hello@firegulaman.com. We'd love to hear from you!