双等于vs在python中
问题描述
我在 Python 解释器中运行以下命令:
I run the following in the Python interpreter:
>>> foo = 10
>>> dir(foo) == dir(10)
True
>>> dir(foo) is dir(10)
False
>>>
这是为什么?
解决方案
is 检查 2 个参数是否引用同一个对象,== 检查 2 个参数是否具有相同的值.dir() 返回一个 list,其中包含 foo 和 10 的相同数据,但实际的 >list 两件事的实例是不同的.
is checks that 2 arguments refer to the same object, == checks that 2 arguments have the same value. dir() returns a list which contains the same data for both foo and 10, but the actual list instances for the 2 things are different.
相关文章