can a class instance ever be hashable?

Aahz Maruch aahz at panix.com
Thu Sep 21 21:53:00 EDT 2000


In article <KKyy5.16386$F9.560863 at news.corecomm.net>, lynx  <a at b.c> wrote:
>Alex <cut_me_out at hotmail.com>, in
><etdog1hnt13.fsf at x15-cruise-basselope.mit.edu>:
>
>> Read about these methods in the Language Reference documentation.  Any
>> class that defines them can be hashable.  In fact you only need to
>> define __hash__, but that will lead to confusing results when you have
>> different instances that you want to regard as equal for some reason.
>
>thanks. i actually did read about these in my copy of the _essential
>reference_ a couple days ago, but it mainly just confused me. i'm
>still struggling with python's idea of mutability and how to tell
>whether a thing is or isn't, and especially with how to solve my
>particular problem so the classes in question are/aren't mutable.

The basic question is whether the attributes that make up the hash can be
changed by the class user after the instance is created.  If the answer
is "yes", then the class is mutable and you can't hash it.  If you're
familiar with SQL DBs, the hash is fundamentally equivalent to a primary
key -- you can change the non-key values once you create a record, but
you're not allowed to change the primary key.  (Yes, I know this isn't a
strictly correct analogy, but it gets the point across, I think.)
-- 
                      --- Aahz (Copyright 2000 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

Member of the Groucho Marx Fan Club  --Aahz



More information about the Python-list mailing list