[Python-Dev] Iterators (PEP 234)

Christian Tismer tismer@tismer.com
Tue, 06 Feb 2001 13:51:37 +0100


"M.-A. Lemburg" wrote:
> 
> Ka-Ping Yee wrote:
<snip/>
> > Three points:
> >
> > 1.  We have syntactic support for mapping creation and lookup,
> > and syntactic support for mapping iteration should mirror it.
> >
> > 2.  IMHO
> >
> >     for key:value in dict:
> >
> > is much easier to read and explain than
> >
> >     for (key, value) in dict.xitems():
> >
> > (Greg?  Could you test this claim with a survey question?)
> >
> > To the newcomer, the former is easy to understand at a surface
> > level.  The latter exposes the implementation (an implementation
> > that is still there in PEP 234, but that the programmer only has
> > to worry about if they are going deeper and writing custom
> > iteration behaviour).  This separates the work of learning into
> > two small, digestible pieces.
> 
> Tuples are well-known basic Python types. Why should
> (key,value) be any harder to understand than key:value.
> What would you tell a newbie that writes:
> 
> for key:value in sequence:
>     ....
> 
> where sequence is a list of tuples and finds that this doesn't
> work ?

Sorry about sneaking in. I do in fact think that the syntax
addition of key:value is easier to understand. Beginners
know the { key:value } syntax, so this is just natural.
Givin him an error in your above example is a step to clarity,
avoiding hard to find errors if somebody has a list of
tuples and the above happens to work somehow, although he
forgot to use .xitems().

> Besides, the items() method has been around for ages, so switching
> from .items() to .xitems() in programs will be just as easy as
> switching from range() to xrange().

It has been around for years, but key:value might be better.
A little faster for sure since we don't build extra tuples.

> I am -0 on the key:value thingie. If you want it as a way to
> construct or split associations, fine. But it is really not
> necessary to be able to iterate over dictionaries.
> 
> > 3. Furthermore, this still doesn't solve the backward-compatibility
> > problem that PEP 234 takes great care to address!  If you write
> > your for-loops
> >
> >     for (key, value) in dict.xitems():
> >
> > then you are screwed if you try to replace dict with any kind of
> > user-implemented dictionary-like replacement (since you'd have to
> > go back and implement the xitems() method on everything).
> 
> Why is that ? You'd just have to add .xitems() to UserDict and
> be done with it. This is how we have added new dictionary methods
> all along. I don't see your point here.

You really wouldn't stick with UserDict, but implement this
on every object for speed.
The key:value proposal is not only stronger through its extra
syntactical strength, it is also smaller in code-size to implement.

Having to force every "iterable" object to support a modified
view of it via xitems() even doesn't look elegant to me.
It forces key/value pairs to go through tupleization only
for syntactical reasons. A weakness, not a strength.
Object orientation gets at its limits here. If access to keys
and values can be provided by a single implementation for
all affected objects without adding new methods, this suggests
to me that it is right to do so.

+1 on key:value   - ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer@tismer.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     where do you want to jump today?   http://www.stackless.com