有人可以澄清这些转让声明背后的逻辑。
>>> 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的唯一两个实例是True
和False
。
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?
提前感谢您的帮助!!!
答案 0 :(得分:0)
当你这样做时:
True = False
True
变为False
..当你这样做时:
True = True
这就像写作:
True = False
如果你想成为"原创"再次True
,你应该:
>>> True = not True # recall that you assigned False to True
>>> True
True