id == vs is

Cameron Simpson cs at zip.com.au
Mon Oct 27 03:36:11 CET 2014


On 27Oct2014 00:41, MRAB <python at mrabarnett.plus.com> wrote:
>On 2014-10-27 00:24, Ethan Furman wrote:
>>On 10/26/2014 05:23 PM, Ethan Furman wrote:
>>>On 10/26/2014 05:12 PM, Dan Stromberg wrote:
>>>>Are the following two expressions the same?
>>>>
>>>>x is y
>>>>
>>>>Id(x) == id(y)
>>>>
>>>>?
>>
>>Listen to MRAB, ignore me.
>>That is all.
>>
>Well, apart of Joshua's qualifications, that is! :-)

Let's make it clear for the OP, since this thread seems to have wandered into 
the realms of the confusing, for what is a basic question.

The short answer is "yes".

The longer answer is:

   if nobody else is fiddling with "x" and "y"
   and if id() is the normal builtin python "id" function

Then yes.

The "is" test is more direct and less subject to iffiness because the longer 
expression using id() leaves more scope/time for things to change, and of 
course "id" itself can be rebound to something weird.

Cheers,
Cameron Simpson <cs at zip.com.au>

The significant problems we face cannot be solved at the same level of
thinking we were at when we created them.       - Albert Einstein



More information about the Python-list mailing list