Common Python Idioms
John Machin
sjmachin at lexicon.net
Thu Dec 7 14:03:19 EST 2006
Daniel Dittmar wrote:
> Duncan Booth wrote:
> > In this case, dict objects used to not support the 'in' operator, but at
> > some point it was added. I believe it wasn't there originally because Guido
> > wasn't sure whether people would expect it should match keys or keys and
> > values.
>
> And he was right:
>
> import sys
> 'sys' in sys.modules => True
> sys in sys.modules => False
>
> Doesn't this look wrong?
No it doesn't look wrong to anyone who has read the docs on
sys.modules. And it's irrelevant. The comparison should be between
'sys' in sys.modules # match keys
and
('sys', sys) in sys.modules # match keys and values
not
sys in sys.modules # match values
In any case, dict values can be any old object and may not even support
an equality test -- matching keys only seems the only reasonable choice
to me.
Cheers,
John
More information about the Python-list
mailing list