On Thu, 22 May 2008, Erick Tryzelaar wrote:
python 2.6 and 3.0 has something similar to this with collections.namedtuple:
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 + p # indexable like the plain tuple (11, 22)
x, y = p # unpack like a regular tuple x, y
p.x + p.y # fields also accessible by name
p # readable __repr__ with a name=value style
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.