Language design

Ned Batchelder ned at nedbatchelder.com
Thu Sep 12 14:37:47 EDT 2013


On 9/12/13 2:24 PM, Markus Rother wrote:
> On 10.09.2013 08:09, Steven D'Aprano wrote:
>> What design mistakes, traps or gotchas do you think Python has? Gotchas
>> are not necessarily a bad thing, there may be good reasons for it, but
>> they're surprising.
> I have one more:
>
> Dictionaries should iterate over their items instead of their keys.

I understand the natural desire for this, and I sometimes forget and 
have to add a ".items()" to my loops.  But if you consider how "in" 
should work with dicts, it's only reasonable that "k in d" examine if a 
key is in a dict, not if an item is in a dict.  And once you have "in" 
working like that, it's reasonable for iteration to work on keys.

Dicts act like collections of keys, each with an associated value.

--Ned.
> Looking forward to contrary opinions.
>
> Greets,
> Markus




More information about the Python-list mailing list