[Python-Dev] bool does not want to be subclassed? (original) (raw)

"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 tphash. 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 object.

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:

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:

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.

Regards, Martin



More information about the Python-Dev mailing list