This question already has an answer here:
- Final classes in Python 3.x- something Guido isn't telling me? 4 answers
I came across the following in the python docs:
bool([x])
Convert a value to a Boolean, using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise it returns True. bool is also a class, which is a subclass of int. Class bool cannot be subclassed further. Its only instances are False and True.
I've never in my life wanted to subclass bool
, but naturally I immediately tried it, and sure enough:
>>> class Bool(bool):
pass
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
class Bool(bool):
TypeError: Error when calling the metaclass bases
type 'bool' is not an acceptable base type
So, the question: How is this done? And can I apply the same technique (or a different one) to mark my own classes as final
, i.e., to keep them from being subclassed?