Change the future

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

Transforming Code into Beautiful, Idiomatic Python

Raymond Hettinger

Audience level:
Novice
Category:
Best Practices/Patterns

Description

Learn to take better advantage of Python's best features and improve existing code through a series of code transformations, "When you see this, do that instead."

Abstract

This fast-paced tutorial is for beginning and intermediate Python programmers who want to write better code. The focus is on improving craftsmanship with a handful of specific code transformations that can be immediately applied to your current projects.

We start by showing how to replace traditional index manipulation with Python's core looping idioms: iter, range/xrange, zip/izip, enumerate, reversed, sorted, and items/iteritems. More advanced techniques include for/else to replace unnecessary state flags and using the two-argument form of iter() to transform function calls into iterators.

Next up are some dictionary manipulation idioms -- using zip with the dict constructor, using get for counting, using setdefault for grouping, and whether to use popitem for atomic dictionary access in multi-threaded code.

We look at how to add clarity to function calls using keyword arguments and how to make code more self-descriptive with named tuples.

Another transformation shows how tuple unpacking can simplify and speed-up traditional indexed lookups. Then we move to a more advanced technique combining tuple packing and unpacking for simultaneous updates of state variables. This important technique can improve code clarity, express high-level thinking, and avoid the pitfalls of the traditional ways of updating state variables.

Next-up, we show-off list comprehensions and generator expressions as a way of writing expressive, high-level code. We explore parallels to Unix pipes and filters, to SQL statements, and to techniques used in mathematics.

Lastly, we'll show how to refactor code using decorators and context managers. Our goal is to cleanly separate administrative logic from business logic while improving code reusability.

If time allows, we'll explore using iterator chains and Python's functional programming tools for making composable, reusable, separately-testable components.