Guido van Rossum wrote:
> On 9/4/07, Noam Raphael <[EMAIL PROTECTED]> wrote:
>> On 9/4/07, Greg Ewing <[EMAIL PROTECTED]> wrote:
>>> Noam Raphael wrote:
>>>> The default dict iterator should in principle be iteritems(), and not
>>>> iterkeys().
>>> This was discussed at length back when "in" support was
>>> added to dicts. There were reasons for choosing to do it
>>> the way it's done, and I don't think it's likely to be
>>> changed.
>>>
>> Just out of curiousity - do you remember these reasons?
> 
> Consistency with "k in d", where you'll agree with me that the only
> useful interpretation is checking for a key. It would be annoying if
> "for x in obj:" no longer rhymed with "if x in obj:".

I would certainly be rather annoyed if the following code could blow up 
with an assertion error in the absence of any threading foolishness:

   for k in d:
       assert k in d

Containment and iteration really do need to be kept consistent and 
having the value matter when checking for dictionary containment would 
be outright bizarre. Put the two together and it makes sense for 
dictionary iteration and containment tests to both be based on keys.

Note that the other basic container types in the standard library 
(lists, tuples, sets, strings, xrange) also obey the 
iteration<->containment invariant above.

Cheers,
Nick.

-- 
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org
_______________________________________________
Python-3000 mailing list
Python-3000@python.org
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to