Question about accessing class-attributes.

Michael Hudson mwh at python.net
Wed Apr 30 04:59:10 EDT 2003


Steven Taschuk <staschuk at telusplanet.net> writes:

> Quoth Michael Hudson:
> > mis6 at pitt.edu (Michele Simionato) writes:
>   [...]
> > > 2) why you say this makes classes unhashable ?
> > 
> > Well, I thought it would.  I was wrong:
>   [...]
> > Then you get this kind of weirdness:
> > 
> > />> class C(type):
> > |..     def __eq__(self, other):
> > |..         return 1
> > \__ 
> 
> Can't the instances of this metaclass be made sensible for use in
> dicts simply with
>     def __hash__(self):
>         return hash(C)
> for example?  (To ensure that if x == y then hash(x) == hash(y),
> as desired.)  

Yes!  The point I was making was that *not* doing that *doesn't* lead
to error messages.

Hang on...

/>> class C(object):
|..  def __eq__(self, other):
|..   return 1
\__ 
->> hash(C())
137007476
/>> class C:
|..  def __eq__(self, other):
|..   return 1
\__ 
->> hash(C())
Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: unhashable instance

Hmm.  I wonder if this is deliberate...

Cheers,
M.

-- 
  MARVIN:  Do you want me to sit in a corner and rust, or just fall
           apart where I'm standing?
                    -- The Hitch-Hikers Guide to the Galaxy, Episode 2




More information about the Python-list mailing list