Discussion about PEP 234: iterators

Jay O'Connor joconnor at cybermesa.com
Fri Feb 16 17:56:38 EST 2001


Tim Hochberg wrote:

> "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.
> ...

>
>
> > 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.

Actually, given my Smalltalk background, I tend to see Dictionaries as
containing Associations (an Assocaiation is a key value pair)  Thus looping over
a Dictionary returns an association on each iteration.

To carry this to Python, I would like to see looping over Dictionaries return a
Tuple of the key,value pair.  Allowing either:

    for assoc in dictionary:
        print "key %s = value %s" % (assoc[0], assoc[1])

or

    for (key, value) in dictionary:
        print "key %s = value %s" % (key,value)

Take care,
Jay O'Connor
joconnor at cybermesa.com




More information about the Python-list mailing list