double equals vs is in python [duplicate]

2019-01-08 10:55发布

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条回答
Rolldiameter
2楼-- · 2019-01-08 11:50

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.

查看更多
登录 后发表回答