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

Joshua Marshall Joshua.Marshall at mathworks.com
Fri Feb 13 18:41:04 EST 2004

[Joshua Marshall]
> I don't think I'm convinced; the same argument could be used for 
> integers (if it doesn't make sense to create a sort of boolean which 
> isn't in the set { true, false }, then it doesn't make sense to create

> a sort of integer which isn't in the set { ..., -2, -1, 0, 1, 2, ... 
> }).  And maybe it doesn't, but this isn't the only reason for 
> subclassing.  Another reason for subclassing is to create items which 
> can act like existing objects, but which have some additional 
> behavior.

> And indeed, for int, it is possible to have subclasses which 
> have new instances whose values are in the set {..., -2, -1, 
> 0, 1, 2, ...}. Indeed, it is possible to have multiple 
> instances of int *itself* whose value is, say, 1000:
>  >>> 500+500 is 500+500
> False
> The same is not true for bool: There are only two *instances* 
> of the type, not just two equivalence classes of equal values:
>  >>> (4>5) is (3>9)
> True
> So bool guarantees: a) there are only two distinct values, 
> and b) there are only two different objects representing 
> these values. It is property b) which prohibits subclassing.

Ah I misread Guido's comment--he's talking about the actual objects, not
what they represent.

So a different question...  Can it be relied upon that two expressions
which both evaluate to False both return the same object?  That is, is
it incorrect for a Python interpreter not to do this?  I find this in
the Python Reference Manual: "for immutable types, operations that
compute new values may actually return a reference to any existing
object with the same type and value" (note the "may").  Are bool and
NoneType the only types for which this is reliably the case?

More information about the Python-Dev mailing list