Python布尔逻辑混乱

时间:2016-04-21 07:03:25

标签: python python-2.7 boolean boolean-logic

有人可以澄清这些转让声明背后的逻辑。

>>> True
True
>>> True = False
>>> True
False
>>> True = True
>>> True
False
>>> a = True
>>> if a:
...     print "a is True"
... else:
...     print "a is False"
...
a is False

根据手册,类bool的唯一两个实例是TrueFalse

Help on bool object:

True = 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

我是否重写了默认实例?如果是这样,为什么python不会将True分配给下面的赋值中的默认实例?如何在以下语句中指定默认的python True

>>> True = False
>>> True
False
>>> True = True
>>> True
False     #why?

提前感谢您的帮助!!!

1 个答案:

答案 0 :(得分:0)

当你这样做时:

True = False

True变为False ..当你这样做时:

True = True

这就像写作:

True = False

如果你想成为"原创"再次True,你应该:

>>> True = not True # recall that you assigned False to True
>>> True
True