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


Groups > comp.lang.python > #39202

Re: Instances as dictionary key, __hash__ and __eq__

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=755c3ee73=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.036
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; 'attribute': 0.05; 'attributes': 0.07; 'iterate': 0.09; 'subclass': 0.09; 'subclasses': 0.09; 'tuple': 0.09; 'cc:addr:python-list': 0.10; 'compare.': 0.16; 'happily': 0.16; 'simplest': 0.16; 'subject:key': 0.16; 'tuple,': 0.16; 'instance': 0.17; 'tim': 0.18; 'are.': 0.22; 'cc:2**0': 0.23; 'class.': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '(e.g.': 0.27; 'hash': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; "i'm": 0.29; 'returned': 0.30; 'know.': 0.33; 'self': 0.34; 'returning': 0.35; 'there': 0.35; 'but': 0.36; 'method': 0.36; 'should': 0.36; 'thank': 0.36; 'uses': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'things': 0.38; 'you.': 0.61; 'subject:, ': 0.61; 'received:194': 0.61; 'information': 0.63; 'more': 0.63; 'making': 0.64; 'person,': 0.65; 'want,': 0.65; 'account': 0.67; 'disclose': 0.69; 'notice:': 0.71; 'privileged.': 0.72; 'delaney': 0.84; 'restrain': 0.84; 'medium.': 0.91
X-IronPort-AV E=Sophos;i="4.84,694,1355094000"; d="scan'208";a="1198557"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Tue, 19 Feb 2013 11:11:57 +0100 (CET)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To Tim Delaney <tim.delaney@aptare.com>
In-Reply-To <CAN8CLgmW+HahHyoM-Jq4mbEc1Tc5=f2HjO7zueQw=d9VoQpK+A@mail.gmail.com>
Subject Re: Instances as dictionary key, __hash__ and __eq__
MIME-Version 1.0
X-Mailer Zimbra 7.2.0_GA_2669 (ZimbraWebClient - GC7 (Linux)/7.2.0_GA_2669)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.2013.1361268773.2939.python-list@python.org> (permalink)
Lines 21
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361268773 news.xs4all.nl 6841 [2001:888:2000:d::a6]:57982
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39202

Show key headers only | View raw


> > Additionally, If I'm making things much more complicated than they
> > need to be, let me know.
> 

> You are. There are ways to achieve what you want, but it requires a
> lot more setup and discipline. The simplest way is probably to have
> a _equal_fields() method that subclasses override, returning a tuple
> of the attributes that should be hashed. Then in __hash__() and
> __eq__ you iterate over the returned tuple, get the value for each
> attribute and either hash or compare.

> Of course, you have to take into account in __eq__ that the other
> instance may not have the same attributes (e.g. self is a subclass
> that uses extra attributes in its __hash__ and __eq__).

> Tim Delaney

I will happily restrain myself to never subclass such class.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

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


Thread

Re: Instances as dictionary key, __hash__ and __eq__ Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-02-19 11:11 +0100

csiph-web