Pythonic way for missing dict keys
Duncan Booth
duncan.booth at invalid.invalid
Sat Jul 21 10:48:10 EDT 2007
"Rustom Mody" <rustompmody at gmail.com> wrote:
> Can someone who knows about python internals throw some light on why
>>>> x in dic
> is cheaper than
>>>> dic.has_key(x)
>
> ??
>
Some special methods are optimised by having a reserved slot in the data
structure used to implement a class. The 'in' operator uses one of these
slots so it can bypass all the overheads of looking up an attribute such as
'has_key'.
More information about the Python-list
mailing list