PyCon 2016 in Portland, Or
hills next to breadcrumb illustration

Saturday 9 a.m.–12:20 p.m.

Pythonic Objects: implementing productive APIs with the Python Data Model

Luciano Ramalho

Audience level:
Intermediate
Category:
Best Practices & Patterns

Description

Python is so consistent that often we can infer the behavior of new objects by assuming they work as the built-ins. The Python Data Model is the foundation of this consistent behavior. This talk presents the construction of Pythonic objects: classes that feel "natural" to a Python programmer, and leverage some of the best language features by implementing key protocols of the Data Model.

Abstract

This tutorial will show how to implement objects which behave as "naturally" as the built-in types, and therefore deserve to be called _Pythonic_. The whole presentation and exercises will be guided by doctests, which support a form of BDD (behavior-driven design) and allow participants to check their progress in the hands-on parts of the tutorial. An API is considered Pythonic when it supports convenient and suitable Python idioms. For example, Python programmers expect that any collection is iterable and supports the `len()` function. Empty collections should evaluate "falsy" in boolean contexts. Objects of any type should have an user-friendly string representation, and another display format that doesn't hide details and is useful for debugging. Objects of several types support operators such as `+` and `*` when it makes sense. Pythonic objects are one of the keys to high programmer productivity with the language. All of these object features, and more, are defined in the Python Data Model: the API that applies to Python objects in general, from plain integers to collections and even to functions and classes -- when we treat them as first class objects in the language. The most important of the special methods defined in the Data Model will be shown and exercised during the tutorial.

Student Handout

No handouts have been provided yet for this tutorial