473,320 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

__contains__ vs has_key() - am I missing a subtle distinction?


I got to wondering if there is a difference between __contains__() and
has_key() for dictionaries (I don't think there is), so I peeked at
UserDict.UserDict and saw they were implemented as distinct methods:

def has_key(self, key):
return self.data.has_key(key)

def __contains__(self, key):
return key in self.data

even though the UserDict implementation explicitly defines self.data as a
true dict. There can be no mistake what the semantics of the two methods
are. I half expected __contains__ to be defined as

__contains__ = has_key

Did I miss some subtle distinction or was the UserDict author just trying to
be as explicit as possible in demonstrating the relationship between methods
and language constructs?

Skip
Jul 18 '05 #1
1 4549
Skip Montanaro wrote:
I got to wondering if there is a difference between __contains__() and
has_key() for dictionaries (I don't think there is), so I peeked at
UserDict.UserDict and saw they were implemented as distinct methods:

def has_key(self, key):
return self.data.has_key(key)

def __contains__(self, key):
return key in self.data

even though the UserDict implementation explicitly defines self.data as a
true dict. There can be no mistake what the semantics of the two methods
are. I half expected __contains__ to be defined as

__contains__ = has_key

Did I miss some subtle distinction or was the UserDict author just trying
to be as explicit as possible in demonstrating the relationship between
methods and language constructs?


I suspect the latter. There are no differences now, and I don't think
any were ever planned from back when __contains__ was introduced. But
asking on python-dev may give you a better chance to hear from the
original author.
Alex

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Anand S Bisen | last post by:
Hello I have been developing a code that works pretty well on my python 2.3 and now when i am running it on my server where it is programmed to run it's giving me errors. I have been using...
77
by: Ville Vainio | last post by:
I tried to clear a list today (which I do rather rarely, considering that just doing l = works most of the time) and was shocked, SHOCKED to notice that there is no clear() method. Dicts have it,...
7
by: David Isaac | last post by:
I have a subclass of dict where __getitem__ returns None rather than raising KeyError for missing keys. (The why of that is not important for this question.) I was delighted to find that...
48
by: Paul Melis | last post by:
Hello, I've always been using the has_key() method to test if a dictionary contains a certain key. Recently I tried the same using 'in', e.g. d = { ... } if k in d: ... and found that it...
5
by: rconradharris | last post by:
A co-worker of mine came across some interesting behavior in the Python interpreter today and I'm hoping someone more knowledgeable in Python internals can explain this to me. First, we create...
3
by: sebastien.lannez | last post by:
Hi everybody, I need to overload the operator in and let him return an object ... It seems it is not a behavior Python expect : .... def __contains__(self,a): .... return 'yop'...
3
by: sebastien.lannez | last post by:
Thanks for your quick response. Using Python as an algebraic parser for symbolic mathematical equation and I need that the 'in' operator returns an object based on its two arguments.
2
by: eboy98 | last post by:
if i have a dictionary name number ....and i want to ask the list whether a particular key already exists. {'octal': '1234567', 'binary': '10100101', 'decimal': '1234567890', 'hexadecimal':...
4
by: William Krick | last post by:
Given these two code snippets, the first generates errors, the second doesn't... // generates errors $tboard = $context; $tboard = empty($tboard)?'':' - '.$tboard; $ttopic = $context; $ttopic...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.