PEP 234: Iterators (fwd)

Dr. David Mertz mertz at gnosis.cx
Tue May 1 14:42:20 EDT 2001


    - There is still discussion about whether

          for x in dict: ...

      should assign x the successive keys, values, or items of the
      dictionary.  ...for sequences, "if x in y" iterates over y
      comparing the iterated values to x.  If we adopt both of the above
      proposals, this will also hold for dictionaries.

This proposal seems to introduce rather *contradictory* semantics for
lists and dictionaries.  I would argue against it on those grounds.

For example, the construct:

    for x in collection:
        print collection[x]

will fail if 'collection' is a list (or sequence, in general).  But in
the proposal this succeeds for dictionaries.  The converse construct:

    for x in collection:
        print collection.index(x)

will fail for dictionaries, but because of the missing '.index()'
method.  But conceptually, dictionaries could easily have an '.index()'
method to implement the following code:

   def index(dct, val):
        for k,v in dct.items():
            if v==val:
                return k

Lists are easily thought of as dictionaries whose keys are an initial
subsequence of the non-negative integers.  The 'in' keyword applied to
lists asks a question about a lists "values" not its "keys"... for both
'if' and 'for'.






More information about the Python-list mailing list