PEP 234: Iterators

Michael Hudson mwh21 at cam.ac.uk
Wed May 2 07:01:38 EDT 2001


"Peter Caven" <p.caven at ieee.org> writes:

> I really like the idea of returning different kinds of iterators for
> dictionaries as described:
> 
> >           for key, value in dict.iteritems(): ...
> >
> >           for value in dict.itervalues(): ...
> >
> >           for key in dict.iterkeys(): ...
> >

Me too.

> But in case nobody has suggested it yet, perhaps the default case:
> 
> for x in dict
> 
> should iterate over items, and that:
> 
> key,value in dict
> 
> would then maintain the desirable symmetry.

I *think* the problem with this is that we want to write:

    if key in dict:
        ...

You're almost never going to want to write "if item in dict", and
having different meanings for "x in dict" in different contexts would
be insane.

On reflection, I think I think (<wink>) of dicts more as containing
keys, and associating data with each key.  I don't think of them as
containing pairs.

> This would open up some further interesting possibilities such as specifying
> either the key or the value
> as an object implementing the __eq__ operation in various ways (regular
> expressions, etc...).
> ... but I'm sure you've all already thought of that: :-)

Eh?  

Cheers,
M.

-- 
  Clue: You've got the appropriate amount of hostility for the
  Monastery, however you are metaphorically getting out of the
  safari jeep and kicking the lions.                         -- coonec
               -- http://home.xnet.com/~raven/Sysadmin/ASR.Quotes.html



More information about the Python-list mailing list