[Python-Dev] bool does not want to be subclassed?

"Martin v. Löwis" martin at v.loewis.de
Mon Feb 16 10:30:25 EST 2004

> Hm.  The Numeric.array object should not have defined __hash__ or
> tp_hash.  It's wrong if a mutable object defines a hash function, for
> exactly this reason.

I think Guido was somewhat too terse here.

Mutable objects should not define hash functions that change depending
if their state changes - a hash must be constant for the life of the

> However, all types derive from object, and object has `__hash__'.
> Consequently, I would be tempted to think that under the new system,
> everything deriving from object is immutable by construction, unless
> tricks are used against it

You are also misinterpreting "defines a hash function". Any object
is hashable, but, unless specifically defined, the hash function uses
the identity of the object (in CPython, its address) to compute the
hash; this won't change if the state changes.

So you should read Guido's statement also as "it's wrong if a mutable
object *specifically* defines a hash function".

> What means a "mutable type" then?

A mutable type is one whose instances have state that can change over
the life of the object.

Of these, there are two categories of objects:
- values: objects whose identity is mostly irrelevant, and who compare
   equal if certain aspects of their state are equal. array.array is
   an example of a value type.
- identity objects: objects whose identity matters in addition to their
   state; different objects will never compare equal.

Identity objects should always be hashable, and their hash should be
based on the identity. Mutability does not matter wrt. to hashing and
dictionary keys.

Values fall into two further categories:
- immutable values: they should define a hash that takes those
   parts of the state into account that is also considered for
   comparison; equal objects should hash equal.
- mutable values: an attempt to hash them should raise an exception,
   as one would require the following, contradicting, properties of
   a hash on such objects:
   - equal objects should hash equal (requires to take state into
   - the hash of an object should never change (requuires not to take
     the state into account).

Dictionaries and array.array instances are mutable values, so they
should have no hash. Types are identity objects; they can hash based
on their identity despite being mutable.


More information about the Python-Dev mailing list