Unnecessary classes
Today I opened a python module that made me feel sad. It has two classes: one of them has 4 methods, all of them static, and no attributes; the other has only one method: __init__
, and 5 attributes.
Why people still write classes like these?
Use functions
…instead of classes with just static methods. If you have this class just to group the functions in a common namespace then create a new module (.py file), this is how python organize namespaces, and it’s good, use it!
Here is the first class I saw today
and here is how I would rewrite it:
I usually just start creating the functions I need, and if I get to this point where I have multiple functions doing related work I just create a module and move them there:
Use builtin data structures
…instead of attributes-only-classes. Here is the other class I saw today:
This is a class being used similar to a struct in C. We could use a dictionary here, and a factory function doing the work __init__
is doing in this example:
Dictionaries are fast, well designed and are always being improved by smart people.
Another interesting builtin data structure is namedtuple. And it has a clever implementation, you should check it out :)
One rule I follow when using dictionaries like the example above is to always modify them with specialized functions. You’ll end up with well defined structures and modules that know how to build and manage these structures inside your application.
What I’m suggesting here is actually the opposite of OO, instead of writing a class with state and methods, keep the state on dictionaries (or tuples, lists, sets) and write functions to manipulate the state. I’ve been using this approach much more than classes lately.
References
Stop writing classes is a great talk by Jack Diederich at python 2012, showing examples where classes are overused.