double equals vs is in python

PythonComparisonBooleanEqualsEquality

Python Problem Overview


I run the following in the Python interpreter:

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

Why is this?

Python Solutions


Solution 1 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbenView Question on Stackoverflow
Solution 1 - PythonSilas RayView Answer on Stackoverflow