class bool(int)
| bool(x) -> bool
|
| Returns True when the argument x is true, False otherwise.
| The builtins True and False are the only two instances of the class bool.
| The class bool is a subclass of the class int, and cannot be subclassed.
|
| Method resolution order:
| bool
| int
| object
True
是bool
,它继承自integer,等于1
(但与1
不完全相同)输出:
但是因为
True
继承自int
,我们应该能够用bool
做任何我们可以用int
做的事情然而,要回答您的main问题,这解释了为什么
1 in (True, None)
的计算结果为True
,因为in
操作符通过检查equality
(使用==
)而不是对象标识(使用is
)来工作。你声明中的None
与此无关;它可以是任何对象,并且您的语句仍将计算为True
True
是1
,但是通过bool
方法添加了一些额外的绒毛我认为实现您所追求的目标的一种python方法是检查对象的标识是否包含在列表中(这是您建议的,但我们可以对其进行一点概括)
本质上,这只是
in
操作符的一个更强大的版本。我们可以把它写成一个函数:相关问题 更多 >
编程相关推荐