PEP 234: Iterators (fwd) 
Author Message
 PEP 234: Iterators (fwd)

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

Sun, 19 Oct 2003 02:42:20 GMT  
 [ 1 post ] 

 Relevant Pages 

1. PEP 234: Iterators (fwd)

2. PEP 234: Iterators

3. PEP 234: Iterators

4. PEP 234: Iterators

5. Discussion about PEP 234: iterators

6. PEP 234 little bug?

7. PEP 276 Simple Iterator for ints (fwd)

8. PEP 276 Simple Iterator for ints (fwd)

9. Jun for Java 234

10. API routine CryptEncrypt() returns error 234

11. PEP 276 Simple Iterator for ints

12. PEP 276 Simple Iterator for ints


Powered by phpBB® Forum Software