令人难以置信的Python布尔功能
>>> a = False
>>> b = False
>>> a | b
True
>>> a
True
>>> b
True
我在 python 解释器中得到了这个。
我只是不这么认为。有没有关于python boolean type
的详细资料?
我使用Python 2.6.6,谢谢!
>>> a = False
>>> b = False
>>> a | b
True
>>> a
True
>>> b
True
I get this in a python interpreter.
I just don't think so. Is there any detailed material about python boolean type
?
I use Python 2.6.6, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只能看到您的问题有意义的一种情况:
开始调试 -
print int(False)
的结果是什么?如果发生上述情况,您应该得到1
。尝试:至于为什么会发生这种情况 - 也许有人对你恶作剧并更改了
False
的值(请参阅这个答案)?我真的想不出还有什么会导致这种情况。您始终可以在需要的模块中将False
设置为bool(0)
,以防止这种情况发生。或者切换到 Python 3,这使得
True
和False
保留字无法更改。I can see only one context in which your problem makes sense:
To start debugging - what's the result of
print int(False)
? If the above happened, you should get1
. Try:As far as why this happened - maybe someone played a prank on you and changed the value of
False
(see this answer)? I really can't think of anything else that would cause this. You could always setFalse
tobool(0)
in modules where you need it, to guard against this.Or switch to Python 3, which makes
True
andFalse
reserved words which can't be changed.你的翻译有问题:
Something is wrong with your interpreter:
|
是 python 中的 按位或 运算符。如果您正在进行条件检查,则应使用
or
运算符:您可以阅读有关按位运算符的更多信息 此处。
编辑/旁注:运行您在问题中发布的代码后,我没有得到相同的结果...您的安装可能有问题...
|
is the bitwise-or operator in python.If you're doing a conditional check you should use the
or
operator:You can read more about bitwise operators here.
Edit/Side Note: After running the code you posted in your question, I am not getting the same results... there may be something wrong with your installation...