Stop Writing Classes

A session at PyCon US 2012

Friday 9th March, 2012

12:10pm to 12:40pm (PST)

Classes are great but they are also overused. This talk will describe examples of class overuse taken from real world code and refactor the unnecessary classes, exceptions, and modules out of them.

Classes must be nouns but not every noun must be a class. If your class only has two methods and one of them is init you probably meant to write a function.
MuffinMail recently refactored their API; it went from 20 classes scattered in 22 modules down to 1 class just 15 lines long. It was a welcome change, but we'll further refactor that down to a single function 3 lines long.

The Python stdlib is an example of a namespace that is relatively flat. You won't find packages that consist of a single module defining an exception, and you won't find many exceptions at all - just 165 kinds in 200k lines of code. That's a tiny ratio compared to most projects including Django.

Of course there are things, like containers, that should be classes. As a final example we'll add a Heap type to the heapq module (admit it, you already have one in your utils.py).

About the speaker

This person is speaking at this event.
Jack Diederich

Python dev, *Con speaker, and serial dot-commer. bio from Twitter

Next session in E3

1:45pm The Magic of Metaprogramming by Jeff Rush

Coverage of this session

Sign in to add slides, notes or videos to this session

Tell your friends!

When

Time 12:10pm12:40pm PST

Date Fri 9th March 2012

Short URL

lanyrd.com/spbzd

Official session page

us.pycon.org/…e/presentation/352/

View the schedule

Share

Topics

See something wrong?

Report an issue with this session