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__

Date 2013-02-19 11:11 +0100
From Jean-Michel Pichavant <jeanmichel@sequans.com>
Subject Re: Instances as dictionary key, __hash__ and __eq__
Newsgroups comp.lang.python
Message-ID <mailman.2013.1361268773.2939.python-list@python.org> (permalink)

Show all headers | 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