Using tuples correctly?

Alex Martelli aleaxit at yahoo.com
Tue Oct 12 09:17:10 CEST 2004


BJörn Lindqvist <bjourne at gmail.com> wrote:
   ...
> So why aren't there a tuple-with-named-attributes type in python? So

Good question.  Tuple-like thingies with named attrs have been
introduced reasonably recently (e.g. as the return types for modules in
the stdlib such as time and os.stat) but not generalized.

> you could write stuff like this:
> 
> return (r: 10, g: 20, b: 30) or maybe return (.r 10, .g 20, .b 30)
> 
> Clearly, I must be thinking wrong or it would already be implemented
> in python. Atleast there would have been a PEP for it. Maybe I'm just

I think it's just that nobody has yet bothered to write a PEP (and do
all the research and experimentation that goes with it).

> using tuples incorrectly and that is why my code uses index access to
> tuples? Someone must have already thought about this and they must
> have discussed it and quickly realized that this is A Very Bad Thing.
> But why?  I'm new, so please dont get to angry if the answer is
> obvious.

It's not obvious to _me_.  In Python 2.5 (too late for 2.4) we COULD
conceivably have such a type, if somebody comes up with a _very_
convincing design for it (the use case is already pretty well
established by the precedents in os.stat and module time, we only need
an obviously great design;-).  New syntax for it, I'd rule out (smells
like the language will be near-frozen in 2.4 -> 2.5, as it was in 2.2 ->
2.3 -- I could be guessing wrong on this, of course).

Something like tuple(r=10, g=20, b=30) won't wash because keyword args
are passed as a dict -- orderless.  Pity, because apart from this it
would be neat... tuple would have to keep a cache of tuple subtypes
indexed by attrnames ('r', 'g', 'b') to avoid making a new type every
time, but that's OK, I think.  Maybe an optional first parameter giving
the ordering, as in tuple(('r','g','b'), r=10,g=20,b=30)?  It being an
exeception if a different ordering is specified for a set of attribute
names which already exists, and default being 'any order you please'...?
But it feels klunky...:-(.  Anyway, this is why the PEP process exists
-- so, a Paladin of the new idea should ask Barry (I think he's still
Pep Czar) for a PEP number and start drafting one.

Use cases and implementation pretty obvious, nothing but a new design to
find... seems pretty simple, as PEPs go...


Alex



More information about the Python-list mailing list