Wandering Thoughts archives

2005-09-09

When Python classes are pointless

This LiveJournal posting wound up raising the question of when one could or should avoid using classes to implement something. In Python, my warning signs are some critical mass of:

  • lots of NonMethodFunctions on the class.
  • either every piece of data is part of the single object, or nothing is.
  • only one object of the class is ever created by the program (as distinct from 'only one copy at any given time, but we throw it away and make a new one every so often').

At a certain point, using a class this way is just hiding the fact that you are writing plain functions that manipulate global variables by tarting things up so it is all 'object oriented'. I would rather that code be clear and honest about what's going on by just doing it. Not infrequently the result is also simpler.

Python gets a small out, since objects are the best way to implement structures, and sometimes a global structure simplifies namespace issues and the code. However, Python modules are also good ways to manage namespaces, so it's not too much of an out.

If you are guessing from this that I really dislike the 'Singleton' design pattern, you would be entirely correct.

python/PointlessClasses written at 00:53:43; Add Comment


Page tools: See As Normal.
Search:
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.