473,473 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sets.Set doesn't honour __eq__

I was expecting the class sets.Set to act like an
unordered list with no two members equal. But, while
the following code prints True, the assertion fails.
from sets import Set

_base = str
class caseless_string(_base):
"""Strings, but equality ignores case."""
def __eq__(self, other):
return _base.__eq__(self.upper(), other.upper())

a = caseless_string('a')
A = caseless_string('A')
print A == a

from sets import Set
assert len(Set([a,A])) == 1
If looks like Set is using dict.__setitem__. In that
case, I reckon that the following is allowed to act in
a surprising fashion. Am I correct?
from sets import Set
foo = 3
bar = 3
if len(Set([foo,bar])) == 2:
print 'Boo!'
Is there an implementation of Set around which checks
for equality, honouring __eq__ ? Any help welcome.

David Vaughan

Need a new email address that people can remember
Check out the new EudoraMail at
http://www.eudoramail.com
Jul 18 '05 #1
1 2040
David Vaughan wrote:
I was expecting the class sets.Set to act like an
unordered list with no two members equal. But, while
the following code prints True, the assertion fails.
from sets import Set

_base = str
class caseless_string(_base):
"""Strings, but equality ignores case."""
def __eq__(self, other):
return _base.__eq__(self.upper(), other.upper())
[...]


You just have to define __hash__ too:

class caseless_string(str):
"""Strings, but equality ignores case."""
def __eq__(self, other):
return str.__eq__(self.lower(), other.lower())
def __hash__(self):
return str.__hash__(self.lower())

works fine for me.

(btw, why do you do the _base trick?)

--
Ciao,
Matteo
Jul 18 '05 #2

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

Similar topics

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,...
31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
3
by: vegetax | last post by:
How can i make my custom class an element of a set? class Cfile: def __init__(s,path): s.path = path def __eq__(s,other): print 'inside equals' return not os.popen('cmp %s %s' %...
9
by: Neil Benn | last post by:
Hello, I can't find the docs for __eq__ on a dict and I can't find a description on what the eq does (strangely it does implement > and < but I have no idea what that does). Does anyone know...
2
by: James Stroud | last post by:
Hello All, I find myself in this situation from time to time: I want to compare two lists of arbitrary objects and (1) find those unique to the first list, (2) find those unique to the second...
14
by: Dennis Benzinger | last post by:
Hi! The following program in an UTF-8 encoded file: # -*- coding: UTF-8 -*- FIELDS = ("Fächer", ) FROZEN_FIELDS = frozenset(FIELDS) FIELDS_SET = set(FIELDS)
5
by: JKPeck | last post by:
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...
2
by: mkppk | last post by:
I have kind of strange change I'd like to make to the sets.Set() intersection() method.. Normally, intersection would return items in both s1 and s2 like with something like this: ...
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
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...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.