Why I can't extend bool in Python?

…衆ロ難τιáo~ 提交于 2019-12-28 12:16:09

问题


>>> 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.


回答1:


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




回答2:


Since the OP mentions in a comment:

I want 1 and 2 to return an instance of my class.

I think it's important to point out that this is entirely impossible: Python does not let you alter built-in types (and, in particular, their special methods). Literal 1 will always be an instance of built-in type int, and in any case the basic semantics of the and operator are not overridable anyway -- a and b is always identical to b if a else a for any a and b (no bool coercion involved, even though the OP appears to mistakenly believe one is happening).

Restating this crucial point: the value of a and b is always, unchangeably either a or b -- there is no way to break this semantic constraint (even if a and b were instances of your own peculiar classes -- even less so of course when they're constrained to be instances of Python's built-in int!-).




回答3:


Here is a post that explains the reasoning behind the decision: http://mail.python.org/pipermail/python-dev/2004-February/042537.html

The idea is that bool has a specific purpose - to be True or to be False, and adding to that would only serve to complicate your code elsewhere.




回答4:


If you are using Python 3, and you want to have a class that can be evaluated as a boolean, but also contain other functionality, implement __bool__ in your class.

In Python 2 the same effect can be achieved by implementing __nonzero__ or __len__ (if your class is a container).




回答5:


Because bool is supposed to only have two values -- True and False. If you were able to subclass bool, you could define arbitrary numbers of values for it, and that's definitely not what you want to happen.

A better question is: why do you want to extend bool?



来源:https://stackoverflow.com/questions/2172189/why-i-cant-extend-bool-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!