473,406 Members | 2,843 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,406 software developers and data experts.

Sets and Membership Tests

I would like to be able use sets where the set members are objects of a
class I wrote.
I want the members to be distinguished by some of the object content,
but I have not figured out how a set determines whether two (potential)
elements are identical. I tried implementing __eq__ and __ne__ and
__hash__ to make objects with identical content behave as identical for
set membership, but so far no luck.

I could subclass set if necessary, but I still don't know what I would
need to override.

TIA for any advice you can offer.

Jul 12 '06 #1
5 1256

JKPeck wrote:
I would like to be able use sets where the set members are objects of a
class I wrote.
I want the members to be distinguished by some of the object content,
but I have not figured out how a set determines whether two (potential)
elements are identical. I tried implementing __eq__ and __ne__ and
__hash__ to make objects with identical content behave as identical for
set membership, but so far no luck.
__eq__ and __hash__ are necessary and sufficient. Code?

-Mike

Jul 12 '06 #2
JK,

You are correct to implement __hash__ and __eq__. The problem is how
you implemented them. Usually your __eq__ method should compare the
necessary attributes of the objects for equality. The __hash__ should
return a 32-bit integer. Your best bet is probably to return a hash of
hashes of your attributes that are used in equality comparison. What
this means is that your attributes used to produce the __hash__ should
also be hashable. This is important yet not immediatly obvious. So you
could for example return hash( (attribute1, attribute2, attribute3) ),
where attribute1, attribute2, attribute3 are all hashable.
Of course, you provided no code and no error messages (the
'SoFarNoLuck' exception is not descriptive enough ; )

Hope this helps

JKPeck wrote:
I would like to be able use sets where the set members are objects of a
class I wrote.
I want the members to be distinguished by some of the object content,
but I have not figured out how a set determines whether two (potential)
elements are identical. I tried implementing __eq__ and __ne__ and
__hash__ to make objects with identical content behave as identical for
set membership, but so far no luck.

I could subclass set if necessary, but I still don't know what I would
need to override.

TIA for any advice you can offer.
Jul 12 '06 #3
Thanks for the advice. Once assured that __hash__ etc was the right
route, I found that using hash() instead of object.__hash__() gave me
stable hash valules. (I am hashing strings that I know to be unique.)

The "no luck" situation was that a set would accept the same object
multiple times, not recognizing that it was truly the same object.
Nick Vatamaniuc wrote:
JK,

You are correct to implement __hash__ and __eq__. The problem is how
you implemented them. Usually your __eq__ method should compare the
necessary attributes of the objects for equality. The __hash__ should
return a 32-bit integer. Your best bet is probably to return a hash of
hashes of your attributes that are used in equality comparison. What
this means is that your attributes used to produce the __hash__ should
also be hashable. This is important yet not immediatly obvious. So you
could for example return hash( (attribute1, attribute2, attribute3) ),
where attribute1, attribute2, attribute3 are all hashable.
Of course, you provided no code and no error messages (the
'SoFarNoLuck' exception is not descriptive enough ; )

Hope this helps

JKPeck wrote:
I would like to be able use sets where the set members are objects of a
class I wrote.
I want the members to be distinguished by some of the object content,
but I have not figured out how a set determines whether two (potential)
elements are identical. I tried implementing __eq__ and __ne__ and
__hash__ to make objects with identical content behave as identical for
set membership, but so far no luck.

I could subclass set if necessary, but I still don't know what I would
need to override.

TIA for any advice you can offer.
Jul 12 '06 #4
JKPeck <JK****@gmail.comwrote:
Thanks for the advice. Once assured that __hash__ etc was the right
route, I found that using hash() instead of object.__hash__() gave me
stable hash valules. (I am hashing strings that I know to be unique.)

The "no luck" situation was that a set would accept the same object
multiple times, not recognizing that it was truly the same object.
Your problem may have been exactly that you were misunderstanding the
nature and functionality of object.__hash__:
>>x='guess what'
id(x)
360720
>>object.__hash__(x)
360720

As you see, what it does is return the id() of the object (that's how
objects are hashed -- if they don't implement __eq__ or __cmp__,
equality comparisons also go to the id()'s, so things work;-) -- unless
their type/class overrides __hash__). Of course, by going directly to
object.__hash__ you're explicitly *avoiding* the override, so...
Alex
Jul 12 '06 #5
JK,
As a general rule, let Python call the "magic" __method__ methods
behind the scenes. So don't call obj.__hash()__ or obj.__len__ or
obj.__le__ just use hash(obj), len(obj) or <=. Of course there are
exceptions, for example when calling the __init__() method of a
supercalass inside the __init__ method of your class and perhaps a few
others...
Nick V.
JKPeck wrote:
Thanks for the advice. Once assured that __hash__ etc was the right
route, I found that using hash() instead of object.__hash__() gave me
stable hash valules. (I am hashing strings that I know to be unique.)

The "no luck" situation was that a set would accept the same object
multiple times, not recognizing that it was truly the same object.
Nick Vatamaniuc wrote:
JK,

You are correct to implement __hash__ and __eq__. The problem is how
you implemented them. Usually your __eq__ method should compare the
necessary attributes of the objects for equality. The __hash__ should
return a 32-bit integer. Your best bet is probably to return a hash of
hashes of your attributes that are used in equality comparison. What
this means is that your attributes used to produce the __hash__ should
also be hashable. This is important yet not immediatly obvious. So you
could for example return hash( (attribute1, attribute2, attribute3) ),
where attribute1, attribute2, attribute3 are all hashable.
Of course, you provided no code and no error messages (the
'SoFarNoLuck' exception is not descriptive enough ; )

Hope this helps

JKPeck wrote:
I would like to be able use sets where the set members are objects of a
class I wrote.
I want the members to be distinguished by some of the object content,
but I have not figured out how a set determines whether two (potential)
elements are identical. I tried implementing __eq__ and __ne__ and
__hash__ to make objects with identical content behave as identical for
set membership, but so far no luck.
>
I could subclass set if necessary, but I still don't know what I would
need to override.
>
TIA for any advice you can offer.
Jul 12 '06 #6

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

Similar topics

21
by: Raymond Hettinger | last post by:
I've gotten lots of feedback on the itertools module but have not heard a peep about the new sets module. * Are you overjoyed/outraged by the choice of | and & as set operators (instead of + and...
0
by: Dave Benjamin | last post by:
Hola, I made a backport of sets.py that will run on Jython 2.1. Here is a diff against the Python 2.3 version of sets.py. The changes were simple, but I may have made a mistake here or there,...
7
by: Steve | last post by:
This post has two parts. First is my feedback on sets. (Hello? Last summer called, they want their discussion thread back...) Second is some questions about my implementation of a partition...
3
by: Chris S. | last post by:
>>> from sets import * >>> Set((1,2,3)) Set() >>> Set() Set() >>> Set((,)) Traceback (most recent call last): File "<input>", line 1, in ? File "sets.py", line 399, in __init__...
3
by: wheel | last post by:
I'm not sure if this is the right venue for Plone questions, if not, could someone ref me to a better one? The discussion groups on the plone site are mailing lists and I'd rather not subscribe...
12
by: David Isaac | last post by:
Is it expected for access to set elements to be much slower than access to list elements? Explanation? Thanks, Alan Isaac 9.806250235714316 3.9823075279120701
5
by: DotNetNewbie | last post by:
Hi, I am developing an application that has to scale and be very efficient, and I am using asp.net membership in my application. I set things up in my Users table (it has extra columns that I...
0
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
I just developed a set of Membership, Role, and Profile providers for the SQLite database, and now I would like to verify they behave as expected. I thought I could find a published set of unit...
8
by: Nick | last post by:
Hi there, Membership.GetNumberOfUsersOnline() works great the first time, then jumps up to the number of users registered in the system. I have tried enumerating through each user individually...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.