Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16850

Re: adding elements to set

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:adding': 0.07; 'python': 0.08; '__name__': 0.09; 'assert': 0.09; 'bug,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.13; "'__main__':": 0.16; 'now).': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'stupidly': 0.16; 'subject:set': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'from:addr:web.de': 0.23; "python's": 0.24; "i'm": 0.26; 'function': 0.27; 'elements': 0.29; 'print': 0.29; 'class': 0.29; 'lot.': 0.30; '(as': 0.31; "i've": 0.31; 'implementing': 0.32; 'implement': 0.32; 'header:X-Complaints-To:1': 0.33; 'there': 0.33; 'object': 0.33; 'to:addr:python-list': 0.34; 'it.': 0.34; 'too': 0.34; 'implies': 0.34; 'surprised': 0.34; 'something': 0.35; 'sets': 0.35; 'equal': 0.36; 'another': 0.37; 'skip:_ 10': 0.37; 'doing': 0.38; 'received:org': 0.38; 'clearly': 0.39; 'to:addr:python.org': 0.40; 'below': 0.63; 'granted': 0.68; '__eq__(self,': 0.84; 'andrea': 0.84; 'other):': 0.84; 'x):': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: adding elements to set
Date Thu, 08 Dec 2011 17:49:42 +0100
Organization None
References <4EE0E72E.2040104@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849e1d.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3432.1323362991.27778.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323362991 news.xs4all.nl 6895 [2001:888:2000:d::a6]:53734
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16850

Show key headers only | View raw


Andrea Crotti wrote:

> I've wasted way too much time for this, which is surely not a Python bug,
> not something that surprised me a lot.
> 
> I stupidly gave for granted that adding an object to a set would first
> check if there are equal elements inside, and then add it.
> 
> As shown below this is not clearly the case..
> Is it possible to get that behaviour implementing another magic method
> in my C class or I just have use another function to check (as I'm doing
> now).
> 
> class C(object):
> 
>      def __init__(self, x):
>          self.x = x
> 
>      def __eq__(self, other):
>          return self.x == other.x
> 
> 
> if __name__ == '__main__':
>      s = set()
>      c1 = C(1)
>      c2 = C(1)
>      assert c1 == c2
>      s.add(c1)
>      s.add(c2)
> 
>      print len(s)

Python's sets are hash-based; you have to implement a __hash__() method for 
the elements that ensures that c1 == c2 implies hash(c1) == hash(c2).

>>> class C(object):                                                  
...     def __init__(self, x): self.x = x
...     def __eq__(self, other): return self.x == other.x
...     def __hash__(self): return hash(self.x)
...
>>> c1 = C(1)
>>> c2 = C(1)
>>> c1 == c2
True
>>> s = set()
>>> s.add(c1)
>>> s.add(c2)
>>> len(s)
1

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: adding elements to set Peter Otten <__peter__@web.de> - 2011-12-08 17:49 +0100

csiph-web