Change the future

Saturday 4:15 p.m.–5 p.m.

Python's Class Development Toolkit

Raymond Hettinger

Audience level:
Intermediate
Category:
Core Python (Language, Stdlib)

Description

This is a short, but thorough tutorial on the Python's built-in toolset for creating classes. We look at commonly encountered challenges and how to solve them using Python.

Abstract

A series of Python class writing challenges are presented in the mildly humorous context of a start-up company employing the "lean startup methodology".

Starting with a simple Python class representing our MVP (minimum viable product), our customers present a series of progressively more onerous feature requests.

Challenges include documentation, shared state, alternate constructors, regular functions in a class namespace, converting attribute access to method access, subclassing challenges, and space-efficiency.

Along the way, we solve problems with new-style classes, docstrings, instance variables, class variables, regular methods, static methods, class methods, properties, name-mangling, and slots.

In addition to covering Python-specific tools, we cover the related design patterns including: the Open-Closed Principle, the Flyweight pattern, when to use getters and setters, choosing the correct constructor, when to optimize for space-efficiency, and anticipating the needs of subclassers.