有没有理由使用“is”?

发布于 2024-10-30 07:51:50 字数 352 浏览 1 评论 0原文

而不是“==”?我知道“是”是什么,它是比较变量的同一性。但你什么时候愿意这样做呢?它对我所做的一切就是造成问题。使用它一段时间后(因为我觉得它使我的代码更具可读性),我并不是向“is”宣战。

有人用它来做“==”不能做的事情吗?我不明白为什么他们不让“is”与“==”相同,就像他们让“and”与“&&”一样如果有人想要指针,他们只需说“id(x) == id(y)”,我们就不会有这种混乱。

这是Python中我不理解的“陷阱”之一,并且让很多新手绊倒。我认为它让人们绊倒的原因是他们不明白为什么它会进行身份比较。重点是什么?

编辑: 感谢您的精彩回答。我认为新人应该学到的是“永远使用‘==’,除非你知道自己在做什么”!

Instead of "=="? I know what "is" is, it is comparing the identity of the variable. But when would you ever want to do that? All it has ever done for me is cause problems. After using it for a while (because I felt it made my code more readable), I am not declaring war on "is".

Does anyone use it for something that "==" wouldn't do? I don't understand why they didn't make 'is' the same thing as '==', like they made 'and' the same as '&&' etc. If someone wanted the pointer, they just have to say "id(x) == id(y)" and we wouldn't have this confusion.

It's one of the "gotcha's" in python that I don't understand, and trip a lot of newbies up. The reason I think it trips people up is that they don't get why it would do an identity comparison. What is the point(er)?

Edit:
Thanks for the great answers. I think what new people should take away is "always use '==' unless you know what you are doing"!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蔚蓝源自深海 2024-11-06 07:51:50

是的,这是有原因的。

当您想要比较对象身份(“相同对象”)而不是对象平等(“相同值”)时。在几乎所有情况下,==对象相等)是正确使用的运算符。 (正如评论中所指出的,我完全跳过的小例子是 x is None 习惯用法 - NoneNoneType 的唯一居民.)

对象身份的一种情况是身份缓存/字典,这是某些代理或“由内而外”情况下的常见模式。在这种情况下,我不希望返回“类似对象”的值,我希望返回“相同对象”的值。

快乐编码。


在 Python 中考虑指针是错误的。 Python 中的值是对象,每个值代表它自己。也就是说,仅当 xy 计算结果为同一个对象时,x is y 才为真。当对象在 python 中传递时,同样的概念也成立——传递对象本身。无需考虑参考资料。

Python 是 Call-by-Sharing/Call-by-Object-Sharing,尽管术语 < a href="http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference" rel="nofollow">"Call-By-Reference" -- 存在于“官方文档位置” --经常被错误地用来描述行为(也许是为了“简化”从其他语言的过渡,在其他语言中该术语也经常被错误地使用)。

Yes, there is a reason.

When you want to compare object-identity ("same object") and not object-equality ("same value"). In almost all cases == (object-equality) is the correct operator to use. (As pointed out in the comments, the trivial case I skipped entirely is the x is None idiom -- None is the sole inhabitant of NoneType.)

One case for object-identity is an identity-cache/dictionary which is a common pattern in some proxy or "inside-out" situations. In this case I don't want the value for "a similar object" back, I want value for "the same object" back.

Happy coding.


Thinking about pointers in Python is wrong. Values in Python are objects and each value represents itself. That is, x is y is only true when x and y evaluate to the same object. When objects are passed in python this same concept holds -- the object itself is passed. No need to think about references.

Python is Call-by-Sharing/Call-by-Object-Sharing although the term "Call-By-Reference" -- which exists in the "official documentation places" -- is often incorrectly used to describe the behavior (perhaps to "ease" the transition from other languages where the term is also often incorrectly used).

铃予 2024-11-06 07:51:50

两者有很大的区别。 is 测试对象身份== 测试相等

考虑这个简单的例子:

print [] == []     # True
print [] is []     # False

There is a huge difference the two. is tests for object identity and == tests for equality.

Consider this simple example:

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