PyCon 2016 in Portland, Or
hills next to breadcrumb illustration

Monday 11:30 a.m.–noon

Refactoring Python: Why and how to restructure your code

Brett Slatkin

Audience level:
Best Practices & Patterns


As programs gain complexity, it becomes harder to add features and fix bugs. Reorganizing code is an effective way to make programs more manageable. This talk will show you Pythonic ways to do the most imporant "refactorings": Extract variables with __nonzero__; Change signatures with *args and **kwargs; Extract fields and classes with @property; Create stateful closures with __call__; and more!


How do you feel about the division of responsibilities between your various functions and classes? Chances are your code would be improved by doing some reorganization. But what's the best way to accomplish this type of restructuring in Python? This talk will present a number of concrete suggestions for how to refactor your Python code effectively. I'll show how Python's unique language features can help with the most important types of refactoring tasks. I'll use code examples to demonstrate how and why each refactoring results in better programs.