Common Python Idioms

Daniel Dittmar daniel.dittmar at sap.com
Fri Dec 8 11:42:56 EST 2006


Marc 'BlackJack' Rintsch wrote:
> If ``in`` shouldn't work with dictionaries, either `__contains__()` must
> be implemented to throw an exception or dictionaries shouldn't be iterable.

I agree completely (in the sense that dictionaries shouldn't be iterable 
directly). Probably even more strongly, at least every time I see some 
code where someone iterates over the keys, only to use the key to look 
up the value (instead if using iteritms).

For strings, the 1:1 relationship between 'in' and iteration has already 
been broken. But then, iteration over strings happens in my code only 
when a pass a string where I should have passed a list of strings.

Daniel





More information about the Python-list mailing list