ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> wxpython-users
wxpython-users
[wxpython-users] Use the 'in' statement, always!
by Chester other posts by this author
May 7 2008 3:27PM messages near this date
Re: [wxpython-users] Static variables in wx.Python? | Re: [wxpython-users] Use the 'in' statement, always!
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")
> >> "orange" in fruit_basket
Traceback (most recent call last):
  File "<stdin> ", line 1, in <module>
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 "<stdin> ", line 1, in <module>
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?
Thread:
Chester
Algirdas Brazas
Chester
Chester
Jorgen Bodde

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved