Why I can't extend bool in Python?

Juanjo Conti picture Juanjo Conti · Jan 31, 2010 · Viewed 7.1k times · Source
>>> class BOOL(bool):
...     print "why?"
... 
why?
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    type 'bool' is not an acceptable base type

I thought Python trusted the programmer.

Answer

Max Shawabkeh picture Max Shawabkeh · Jan 31, 2010

Guido's take on it:

I thought about this last night, and realized that you shouldn't be allowed to subclass bool at all! A subclass would only be useful when it has instances, but the mere existance of an instance of a subclass of bool would break the invariant that True and False are the only instances of bool! (An instance of a subclass of C is also an instance of C.) I think it's important not to provide a backdoor to create additional bool instances, so I think bool should not be subclassable.

Reference: http://mail.python.org/pipermail/python-dev/2002-March/020822.html