What's the difference ?

kyosohma at gmail.com kyosohma at gmail.com
Wed Aug 29 16:03:19 CEST 2007

On Aug 29, 8:39 am, Alex <alexandre.ba... at gmail.com> wrote:
> Hye,
> I was just wondering what is the difference between
> >> if my_key in mydict:
> >>     ...
> and
> >> if mydict.has_keys(my_key):
> >>     ...
> I've search a bit in the python documentation, and the only things I
> found was that they are "equivalent".
> But in this (quiet old) sample ( "http://aspn.activestate.com/ASPN/
> Cookbook/Python/Recipe/59875" ), there is difference between the two
> notation.
> Thanks in advance

Weird. Hetland's book, "Beginning Python" states that it's a matter of
taste. Martelli's "Python Cookbook 2nd Ed." says to use the get()
method instead as you never know if a key is in the dict.  However, I
can't seem to find any reference to has_key in his book.

According to Chun in "Core Python Programming", has_key will be
obsoleted in future versions of Python, so he recommends using "in" or
"not in".

There's your non-answer. Hope that helps.


More information about the Python-list mailing list