double equals vs is in python [duplicate]

荒凉一梦 提交于 2019-11-26 09:24:09

问题


This question already has an answer here:

  • Is there a difference between “==” and “is”? 14 answers
  • String comparison in Python: is vs. == [duplicate] 4 answers

I run the following in the Python interpreter:

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

Why is this?


回答1:


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.



来源:https://stackoverflow.com/questions/15008380/double-equals-vs-is-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!