[Python-Dev] bool does not want to be subclassed? (original) (raw)
François Pinard pinard at iro.umontreal.ca
Mon Feb 16 09:37:38 EST 2004
- Previous message: [Python-Dev] Re: bool does not want to be subclassed?
- Next message: [Python-Dev] bool does not want to be subclassed?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[Tim Peters]
>> Type objects are also singletons (e.g., type(True) returns the same >> object as type(False); ditto type("True") and type("False") and >> type("xyz"); etc).
[Guido] > But types are not immutable, so their semantics are different anyway.
I never thought of type objects as being mutable [...]
I'm not sure of the meaning of "immutability of a type". Looking around, I found out this other message from Guido (quoted in its entirety in case the context changes the wanted meaning of words):
----------------------------------------------------------------------> From: Guido van Rossum <guido at python.org> Date: Thu, 18 Oct 2001 21:00:28 GMT Subject: Re: hashval and Numpy To: python-list at python.org Newsgroups: comp.lang.python
Michael Hudson <mwh at python.net> wrote:
|>>> a = Numeric.array([1,2]) |>>> hash(a) | 56 |>>> d = {a:1} # a will be in the 56%8-th slot (in 2.2, anyway) |>>> a[0] = 2 |>>> hash(a) | 57 |>>> d[a] # looks in 57%8-th slot - which is empty | KeyError |>>> b = Numeric.array([1,2]) |>>> hash(b) | 56 |>>> d[b] # looks in 56%8-th slot - but what's there is not eq to b! | KeyError | Moral: don't use mutable objects as dictionary keys.
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. ----------------------------------------------------------------------<
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, like maybe, an intermediate class overriding
hash' with some function with the specific goal of raising an
exception. Is that so?
What means a "mutable type" then?
- Previous message: [Python-Dev] Re: bool does not want to be subclassed?
- Next message: [Python-Dev] bool does not want to be subclassed?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]