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

François Pinard pinard at iro.umontreal.ca
Mon Feb 16 08:37:25 EST 2004


[Guido van Rossum]
> > > there are only two different objects representing these values.

> > Why is this property so important?

> > I can understand why it's useful for there to be only one None,
> > since programs do 'x is None' a lot. But it's not as if you can get
> > away with saying 'x is True' or 'x is False' all over the place.

> Why is it important *not* to have this property?  Maintaining it is
> trivial, and it helps the implementation a bit (it's actually
> *natural* to have only the two instances).

It is not so _important_ *not* to have this property.  However, if it
did not have this property, this would allow for sub-classing `bool',
something useful at times, as I tried to explain in previous messages.

The usefulness of sub-classing `bool' would likely not be worth any
significant slow-down of the implementation.  Yet, if the slowdown was
not to be significant, than it might be worth removing the restriction
about `bool' not allowing sub-classing, so making it more like `int',
and most other types.

I was surprised at first to see that `bool' could not be subclassed, and
so, I asked why; the explanations given on this list have been helpful.
On the other hand, Python should _ideally_ try to limit the number of
limitations which surprise users.  That ideal is difficult, as Python
has many complex corners when you dive a bit under the surface, and some
of these complexities just peek through the surface, once in a while.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard



More information about the Python-Dev mailing list