<分区>
我在 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.
我这辈子从来没有想过子类化 bool
,但我自然而然地立即尝试了一下,果然如此:
>>> 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
那么,问题是:这是如何完成的?我能否应用相同的技术(或不同的技术)将我自己的类标记为 final
,即防止它们被子类化?