Tuesday, April 05, 2011

PyCon: Fun with Python's Newer Tools

Fun with Python's Newer Tools

collections.namedtuple works just like normal tuples, but it lets you assign a name to each field. It's fast, and there is no additional per-tuple space cost.

There's a lot of cool stuff in the collections package.

Named tuples can be used for "instance prototypes" by using "other_tuple._replace(field=5)". Don't be discouraged by the "_"--they had to do that so as to avoid a namespace conflict.

You can subclass a named tuple. It's based on __slots__.

In Python 3.2, there is a functools.lru_cache. It's a decorator. It accepts a maxsize argument.

Side note: I missed the first five minutes of the talk.