The initial e-mail had a typo. Here is its fix…
The has_key() method is limited as opposed to the ‘in’ statement! I have a proof.
TUPLE (‘in’ statement): WORKS!
fruit_basket = (“banana”, “orange”)
“orange” in fruit_basket
True
TUPLE (has_key method): NOT WORKING!
fruit_basket = (“banana”, “orange”)
fruit_basket.has_key(“orange”)
Traceback (most recent call last):
File “”, line 1, in
AttributeError: ‘tuple’ object has no attribute ‘has_key’
LIST (‘in’ statement): WORKS!
fruit_basket = [“banana”, “orange”]
“orange” in fruit_basket
True
LIST (has_key method): NOT WORKING!
fruit_basket = [“banana”, “orange”]
fruit_basket.has_key(“orange”)
Traceback (most recent call last):
File “”, line 1, in
AttributeError: ‘list’ object has no attribute ‘has_key’
DICTIONARY (‘in’ statement): WORKS!
fruit_basket = {“banana”: 123, “orange”: 456}
“orange” in fruit_basket
True
DICTIONARY (has_key method): WORKS!
fruit_basket = {“banana”: 123, “orange”: 456}
fruit_basket.has_key(“orange”)
True
As
you see, the ‘in’ statement gives more clean code and works on tuples,
lists, and dictionaries. The poor old has_key() method works only on
dictionary objects. No wonder Guido removed this stupid method out of
Python in version 3.0. And you still prefer the has_key method?