Question about accessing class-attributes.

Thomas Heller theller at python.net
Wed Apr 30 05:18:52 EDT 2003


Michael Hudson <mwh at python.net> writes:

> 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...
> 

See also http://www.python.org/sf/660098

Thomas




More information about the Python-list mailing list