What past problems of mine the collections module solves

March 7, 2009

The somewhat new collections module has a number of useful things that obsolete several of the things I've done by hand in the past. For my own reference if nothing else, here's more or less what its solutions obsolete and when each solution was introduced.

  • defaultdict obsoletes my use of dict.setdefault(). It was introduced in Python 2.5.

  • namedtuple obsoletes some but not all of my various forms of structures in Python. It was introduced in Python 2.6.

The reason namedtuple doesn't cover all of my uses of (abstract) structures with named fields is right there in the name. Since it is based on tuples, you can't have modifyable structures; this makes namedtuple a good match for functions that want to return structured read-only information, but not for more general uses where you want ordered named fields that you can change.

(I suspect that the Pythonic view is that one should just go straight to full structures and add ordering there, instead of trying to hijack lists. A proper discussion of the issue does not fit within the margins of this entry.)

Unfortunately, I'm not going to get to use the collections module very much any time soon; most of our systems are old enough that they are running Python 2.4, and that's not likely to change. Even the more recent and fast-moving ones are only on Python 2.5, and based on the speed of updates around here, it will probably be at least a year before I'm using Python 2.6 anywhere.

(For the curious: Solaris 10, Red Hat Enterprise 5, and Ubuntu 6.06 all have Python 2.4 and are almost sure to stay that way for their lifetimes. Ubuntu 8.04 and Fedora 10 have Python 2.5, although it looks like the next version of Fedora will have 2.6.)

Written on 07 March 2009.
« What can keep a ZFS pool busy and prevent it from exporting
Python's theoretically missing core data type »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Sat Mar 7 02:16:17 2009
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.