Discussion about PEP 234: iterators

Nathaniel Gray n8gray at caltech.edu.is.my.email.address
Sun Feb 18 03:29:38 EST 2001


Raymond Hettinger wrote:

> I think dictionary iteration should match iterations on other
> objects/types as closely as possible unless performance is impacted.
> 
> for elem in list                # does not automatically sort
> for key in dict                # so this should not automatically sort

What about 
for value in dict          # Should this sort?

> for key:value in dict        # proposed
> for index:item in list        # so this should work
> 
> if elem in list                # this works
> if key in dict                # so this should work

Don't forget
if value in dict

> map( fun, list )            # this works
> map( func, dict )        # so this should work with functions that take a
> key as a parameter

And those that take a value as a parameter too, right?

Don't worry, I'm sure RMS can throw something together with soundex that'll 
be able to tell when you meant keys and when you meant values.

I know I'm being a stickler, but the default of "if thing in dict" is not 
at all clear to me, and it appears I'm not the only one.  Anecdotal 
evidence from Thomas Wouters (I believe) suggests that a significant number 
of programmers expect "thing" to be a value, not a key.  Others expect a 
(key, value) pair.  It just seems there's no clear default.  Oh, and some 
guy named Guido used to agree with me on this one too...

Unlike many of the other folks in opposition to "if key in dict", I'm not 
totally turned off by the "for key:value in dict" syntax and all of its 
variants.  The syntax is easy to learn and it's explicit, if shorthand, for 
the programmer's intent.  Why not use the same syntax for the "if key:value 
in dict" version?  That way nobody'll be happy!  ;^)

(except for me, but I won't complain)

-n8

-- 
_.~'^`~._.~'^`~._.~'^`~._.~'^`~._.~'^`~._
             Nathaniel Gray
   California Institute of Technology
     Computation and Neural Systems
     n8gray <at> caltech <dot> edu
_.~'^`~._.~'^`~._.~'^`~._.~'^`~._.~'^`~._




More information about the Python-list mailing list