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


Groups > comp.lang.python > #16849

Re: adding elements to set

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.020
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'check.': 0.07; 'subject:adding': 0.07; 'python': 0.08; 'bug,': 0.09; 'ids': 0.09; 'am,': 0.12; 'def': 0.13; 'duplicates': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "objects'": 0.16; 'stupidly': 0.16; 'subject:set': 0.16; 'unhashable': 0.16; 'wrote:': 0.18; 'appears': 0.19; 'dec': 0.22; 'stuff': 0.22; 'header:In-Reply-To:1': 0.22; 'default,': 0.23; '(in': 0.26; 'message-id:@mail.gmail.com': 0.28; 'elements': 0.29; 'class': 0.29; 'equality': 0.30; 'hash': 0.30; 'lot.': 0.30; "i've": 0.31; 'objects': 0.32; "can't": 0.32; 'there': 0.33; 'object': 0.33; 'fri,': 0.34; 'to:addr:python-list': 0.34; 'it.': 0.34; 'too': 0.34; 'set.': 0.34; 'surprised': 0.34; 'something': 0.35; 'equal': 0.36; 'checks': 0.37; 'two': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.37; 'using': 0.38; 'received:209.85': 0.38; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'more': 0.61; '2011': 0.61; 'your': 0.61; 'granted': 0.68; 'received:209.85.216.174': 0.80; '__eq__(self,': 0.84; 'alike,': 0.84; 'andrea': 0.84; 'child,': 0.84; 'other):': 0.84; 'x):': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=dqRmg/Qjjot504zFuEnHgxbPb/4I8P2KBfEr9aNWYgA=; b=Da6qYIVAjLNI0ZlDaeJWYvUeSFnPmP20a4DeYqF+BmwQOf3BCUBfQXZmL2Wc/Ujd8/ n6R72P+/YZkwYyQsiEdcu2VbGucAy//RPeKV0zuBLiOeL/gCVUQot8ccns5eLcq0sUoU FWL4PLzTtGYXqGN3ZOiM/898HqFPKyAlU7sKo=
MIME-Version 1.0
In-Reply-To <4EE0E72E.2040104@gmail.com>
References <4EE0E72E.2040104@gmail.com>
Date Fri, 9 Dec 2011 03:47:24 +1100
Subject Re: adding elements to set
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.3431.1323362848.27778.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323362848 news.xs4all.nl 6912 [2001:888:2000:d::a6]:51008
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16849

Show key headers only | View raw


On Fri, Dec 9, 2011 at 3:34 AM, Andrea Crotti <andrea.crotti.0@gmail.com> 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.

It checks for equality using hashes. By default, in Python 2, objects'
hashes are their ids - meaning that no two of them hash alike, and
you'll get duplicates in your set. (In Python 3, the default appears
to be that they're unhashable and hence can't go into the set at all.)

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)

This chains the hashing requirement to the child, just as it chains
the equality check. You can then stuff your objects into a set with
more expected results.

ChrisA

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


Thread

Re: adding elements to set Chris Angelico <rosuav@gmail.com> - 2011-12-09 03:47 +1100

csiph-web