令人难以置信的Python布尔功能

发布于 2024-10-11 23:41:00 字数 261 浏览 1 评论 0原文

>>> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此生挚爱伱 2024-10-18 23:41:00

我只能看到您的问题有意义的一种情况:

>>> False = True
>>> a = False
>>> b = False
>>> a | b
True
>>> a
True
>>> b
True
>>> 

开始调试 - print int(False) 的结果是什么?如果发生上述情况,您应该得到 1。尝试:

>>> False = bool(0)
>>> a = False
>>> b = False
>>> a | b
False

至于为什么会发生这种情况 - 也许有人对你恶作剧并更改了 False 的值(请参阅这个答案)?我真的想不出还有什么会导致这种情况。您始终可以在需要的模块中将 False 设置为 bool(0),以防止这种情况发生。

或者切换到 Python 3,这使得 TrueFalse 保留字无法更改。

I can see only one context in which your problem makes sense:

>>> False = True
>>> a = False
>>> b = False
>>> a | b
True
>>> a
True
>>> b
True
>>> 

To start debugging - what's the result of print int(False)? If the above happened, you should get 1. Try:

>>> False = bool(0)
>>> a = False
>>> b = False
>>> a | b
False

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 set False to bool(0) in modules where you need it, to guard against this.

Or switch to Python 3, which makes True and False reserved words which can't be changed.

清醇 2024-10-18 23:41:00

你的翻译有问题:

Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> False | False
False
>>> a = False
>>> b = False
>>> a | b
False

Something is wrong with your interpreter:

Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> False | False
False
>>> a = False
>>> b = False
>>> a | b
False
可是我不能没有你 2024-10-18 23:41:00

| 是 python 中的 按位或 运算符。

如果您正在进行条件检查,则应使用 or 运算符:

>>> a = False
>>> b = False
>>> a or b
False
>>> a
False
>>> b
False

您可以阅读有关按位运算符的更多信息 此处

编辑/旁注:运行您在问题中发布的代码后,我没有得到相同的结果...您的安装可能有问题...

| is the bitwise-or operator in python.

If you're doing a conditional check you should use the or operator:

>>> a = False
>>> b = False
>>> a or b
False
>>> a
False
>>> b
False

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文