On Thu, 22 May 2008, Erick Tryzelaar wrote:
python 2.6 and 3.0 has something similar to this with collections.namedtuple:
http://docs.python.org/dev/3.0/library/collections.html#collections.namedtup...
The interface is a bit more verbose though:
Point = namedtuple('Point', 'x y') p = Point(11, y=22) # instantiate with positional or keyword arguments p[0] + p[1] # indexable like the plain tuple (11, 22) 33 x, y = p # unpack like a regular tuple x, y (11, 22) p.x + p.y # fields also accessible by name 33 p # readable __repr__ with a name=value style Point(x=11, y=22)
I like the syntax of using arguments to object though. Maybe there's a discussion behind namedtuple why they went that way instead of this one?
Just like lambdas were gotten rid of in favor of named inner functions (in part) for readability, it sounds like readability would be a strong argument for using namedtuple rather than some kind of an anonymous object. -- Cheers, Leif