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


Groups > comp.lang.python > #16847

adding elements to set

Date 2011-12-08 16:34 +0000
From Andrea Crotti <andrea.crotti.0@gmail.com>
Subject adding elements to set
Newsgroups comp.lang.python
Message-ID <mailman.3429.1323362098.27778.python-list@python.org> (permalink)

Show all headers | View raw


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)

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


Thread

adding elements to set Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-12-08 16:34 +0000

csiph-web