Discussion about PEP 234: iterators

Tim Hochberg tim.hochberg at ieee.org
Fri Feb 16 23:39:16 CET 2001


"Raymond Hettinger" <othello at javanet.com> wrote in message
news:3A8DA52A.254849DB at javanet.com...
> I think dictionary iteration should match iterations on other
objects/types
> as closely as possible unless performance is impacted.

Ok then, let me put on my pedantic hat.
.
.
Oof, that's a tight fit.

> for elem in list                # does not automatically sort
> for key in dict                # so this should not automatically sort
>
> for key:value in dict        # proposed
> for index:item in list        # so this should work

Alright, looking at what you have so far, you've established a one to one
correspondence between dictionary keys and sequence indices as well as
between dictionary values and sequence values. That seems to make sense, but
this next bit is where we run into trouble -- consistency runs head on into
practicality and peoples expectations.

> if elem in list                # this works
> if key in dict                # so this should work

If one is going to go by the correspondence that you've set up above, then
really the dictionary equivalent should be:

if value in dict:

Unfortunately, people tend to think of a dictionary as containing keys more
than they think of it as containing definitions (values). So the form you
propose is better from that point of view. It's also probably more useful in
a practical sense as well. It is kind of distressing that it's not
consistent with the correspondence that's set up between keys<->indices and
values<->values though.

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

This is not obvious to me. I would think that this would need a function
that takes values as parameters and would produce:

{key1 : func(val1), key2 : func(val2) ....}

I can't even figure out how this would work if func operated on keys. That
seems to be a bad sign for the obviousness of this construct.....

> filter( boolfun, list )        # gives a possibly smaller list
> filter( boolfun, dict )        # so this should return a possibly smaller
> dictionary

But how would it work? If were to guess at the meaning of something like
this I'd get:

dict = {}
for key, value in dict.keys():
   if boolfun(value):
      dict[key] = value

But being as we disagreed above, I'm not sure that we agree here.

I too would like to see everything as consistent as possible (but no
consistenter[sic]), but I'm not sure that agreement can be reached on that.

-tim







More information about the Python-list mailing list