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


Groups > comp.lang.python > #39139

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!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.03; 'bug.': 0.07; 'class,': 0.07; 'python': 0.09; 'be:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'solution,': 0.09; 'terry': 0.09; 'def': 0.10; 'thread': 0.11; 'keys.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'set,': 0.16; 'subject:key': 0.16; 'wrote:': 0.17; 'basically': 0.17; 'jan': 0.18; 'code,': 0.18; '(or': 0.18; 'cheers,': 0.23; 'idea': 0.24; 'testing': 0.24; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'wrote': 0.26; 'values': 0.26; 'ago': 0.27; 'order.': 0.27; 'header:X-Complaints-To:1': 0.28; 'all.': 0.28; 'cases.': 0.29; 'container': 0.29; 'dictionary': 0.29; 'equality': 0.29; 'hash': 0.29; 'no,': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; 'function': 0.30; 'could': 0.32; 'instances': 0.33; 'point,': 0.33; 'safely': 0.33; 'to:addr:python-list': 0.33; 'equal': 0.33; 'know.': 0.33; 'another': 0.33; 'pm,': 0.35; 'something': 0.35; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'thank': 0.36; 'bad': 0.37; 'skip:v 20': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'object': 0.38; 'things': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'you.': 0.61; 'subject:, ': 0.61; 'containing': 0.61; 'back': 0.62; 'information': 0.63; 'more': 0.63; 'confirm': 0.64; 'making': 0.64; 'person,': 0.65; 'disclose': 0.69; 'notice:': 0.71; 'obvious': 0.71; 'privileged.': 0.72; 'received:fios.verizon.net': 0.84; 'medium.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Instances as dictionary key, __hash__ and __eq__
Date Mon, 18 Feb 2013 18:39:57 -0500
References <1349647696.6217309.1361216182980.JavaMail.root@sequans.com> <662405274.6218650.1361217108266.JavaMail.root@sequans.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2
In-Reply-To <662405274.6218650.1361217108266.JavaMail.root@sequans.com>
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.1981.1361230827.2939.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361230827 news.xs4all.nl 6989 [2001:888:2000:d::a6]:52960
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39139

Show key headers only | View raw


On 2/18/2013 2:51 PM, Jean-Michel Pichavant wrote:
> Greetings,
>
> I opened something like a month ago a thread about hash functions and how I could write classes which instances can be safely used as dictionary keys.
> I though I had it but when I read back my code, I think I wrote yet another bug.
>
> Consider the following simple (buggy) class, python 2.5
>
> class FooSet(object):
>      """Define an algorithm set, containing pdcch/pdsch (or none)."""
>      def __init__(self, pdcch, pdsch):
>          self.pdcch = bool(pdcch)
>          self.pdsch = bool(pdsch)
>      # __hash__ and __eq__ allow to use the object as a dictionary key
>      def __hash__(self):
>          return hash((self.pdcch, self.pdsch))
>      def __eq__(self, other):
>          return hash(self) == hash(other)
>
> Can you confirm that using the hash function for testing equality is a very bad idea ?
>
> One obvious solution would be:
>
> def __eq__(self, other):
>      return self.pdsch = other.pdsch and self.pdcch == other.pdcch
>
> But I was looking for a "standard" solution, that I could use for basically all my container classes
>
> So I came up with these ones:
>
> def __hash__(self):
>      return hash(tuple(vars(self).values()))
> def __eq__(self, other):
>      return vars(self) == vars(other)
>
> But I'm not sure about vars(self).values(), I don't really care about the order of the values, but I need to be sure that for 2 equal dictionaries, they will both return their values in the same order.

No, you cannot depend on that in general even though it may work in 
specific cases.

> And that's the point, I'm not sure at all.
>
> Additionally,  If I'm making things much more complicated than they need to be, let me know.
>
> Cheers,
>
> 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.
>


-- 
Terry Jan Reedy

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


Thread

Re: Instances as dictionary key, __hash__ and __eq__ Terry Reedy <tjreedy@udel.edu> - 2013-02-18 18:39 -0500

csiph-web