双等于 vs 在 python 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15008380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:04:43  来源:igfitidea点击:

double equals vs is in python

pythoncomparisonbooleanequalsequality

提问by ben

I run the following in the Python interpreter:

我在 Python 解释器中运行以下命令:

>>> foo = 10
>>> dir(foo) == dir(10)
True
>>> dir(foo) is dir(10)
False
>>> 

Why is this?

为什么是这样?

采纳答案by Silas Ray

ischecks that 2 arguments refer to the same object, ==checks that 2 arguments have the same value. dir()returns a listwhich contains the same data for both fooand 10, but the actual listinstances for the 2 things are different.

is检查 2 个参数是否指向同一个对象,==检查 2 个参数是否具有相同的值。 dir()返回一个list包含两个相同的数据foo10,而实际list的事情2实例是不同的。