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


Groups > comp.lang.python > #16847

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!post.news.xs4all.nl!not-for-mail
Return-Path <andrea.crotti.0@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'subject:adding': 0.07; 'python': 0.08; '__name__': 0.09; 'assert': 0.09; 'bug,': 0.09; 'def': 0.13; "'__main__':": 0.16; 'now).': 0.16; 'stupidly': 0.16; 'subject:set': 0.16; 'received:74.125.82.44': 0.16; 'received :mail-ww0-f44.google.com': 0.16; "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; 'message- id:@gmail.com': 0.33; 'header:User-Agent:1': 0.33; 'there': 0.33; 'object': 0.33; 'to:addr:python-list': 0.34; 'it.': 0.34; 'too': 0.34; 'surprised': 0.34; 'received:74.125.82': 0.35; 'something': 0.35; 'equal': 0.36; 'received:74.125': 0.37; 'received:google.com': 0.37; 'another': 0.37; 'skip:_ 10': 0.37; 'doing': 0.38; 'received:10.0.0': 0.38; 'clearly': 0.39; 'to:addr:python.org': 0.40; 'below': 0.63; 'granted': 0.68; '__eq__(self,': 0.84; 'other):': 0.84; 'x):': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=9zeNaLI7CBx03bqI32jZsIlyqmRgKlbcPe196DjZ3Fc=; b=oVzAEjNkRO5PKb2/ZjrnHKPuK/ukTq8VRINh1a8asDIj5hHIaFczsitt3ZJcvd3FQ2 xT7IZZQ4T+monz9IZ/WnQ+HpFa3XtUpsYCoCvo68RRkjn3aeDaBsincJs/EVF8kQ/4IK 4be/pOOFJpFHg83EXdMK2SMjn/TAkNllgqeW0=
Date Thu, 08 Dec 2011 16:34:54 +0000
From Andrea Crotti <andrea.crotti.0@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111108 Thunderbird/8.0
MIME-Version 1.0
To python-list@python.org
Subject adding elements to set
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
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.3429.1323362098.27778.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323362098 news.xs4all.nl 6840 [2001:888:2000:d::a6]:50513
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16847

Show key headers only | 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