Re: id == vs is

2014-10-27 Thread Roy Smith
In article ,
 Cameron Simpson  wrote:

> 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.

Not to mention that Python is case-sensitive and in the code as 
presented by the OP:

Id(x) == id(y)

those are two different functions :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: id == vs is

2014-10-26 Thread Ben Finney
Ben Finney  writes:

> Dan Stromberg  writes:
> > Are the following two expressions the same?
[…]
>
> It depends what you mean by “the same”.

My apologies, I mis-read the question. My answers were for a different
question (one you didn't ask). Please ignore that.

-- 
 \  “If you ever reach total enlightenment while you're drinking a |
  `\  beer, I bet it makes beer shoot out your nose.” —Jack Handey |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: id == vs is

2014-10-26 Thread Cameron Simpson

On 27Oct2014 00:41, MRAB  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 

The significant problems we face cannot be solved at the same level of
thinking we were at when we created them.   - Albert Einstein
--
https://mail.python.org/mailman/listinfo/python-list


Re: id == vs is

2014-10-26 Thread Denis McMahon
On Sun, 26 Oct 2014 17:12:29 -0700, Dan Stromberg wrote:

> Are the following two expressions the same?
> 
> x is y
> 
> Id(x) == id(y)

No, although if "Id" and "id" were the same function, they might be 
equivalent in some cases. 

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: id == vs is

2014-10-26 Thread Ben Finney
Dan Stromberg  writes:

> Are the following two expressions the same?
>
> x is y
>
> Id(x) == id(y)

It depends what you mean by “the same”.

Do they give the same result? Sometimes yes, sometimes no. It depends on
what the types of the values are.

Do they express the same intent? Always no. The first queries object
identity, the second queries object equality.

-- 
 \  “Ridicule is the only weapon which can be used against |
  `\   unintelligible propositions.” —Thomas Jefferson, 1816-07-30 |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: id == vs is

2014-10-26 Thread MRAB

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! :-)
--
https://mail.python.org/mailman/listinfo/python-list


Re: id == vs is

2014-10-26 Thread Ethan Furman

On 10/26/2014 05:12 PM, Dan Stromberg wrote:

Are the following two expressions the same?

x is y

Id(x) == id(y)

?


Nope.  If the value if `id(x)` is not interned, then the two value could be different objects that still represent the 
same value.


--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


Re: id == vs is

2014-10-26 Thread Ethan Furman

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.

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


Re: id == vs is

2014-10-26 Thread Joshua Landau
On 27 October 2014 00:12, Dan Stromberg  wrote:
> Are the following two expressions the same?
>
> x is y
>
> Id(x) == id(y)

Much of the time, but not all the time. The obvious exception is if
"id" is redefined, but that one's kind of boring. The real thing to
watch out for is if the object that "x" points to is garbage collected
before "y" is evaluated:

   nothing = "!"

id("hello" + nothing) == id("hello" + nothing)
#>>> True

("hello" + nothing) is ("hello" + nothing)
#>>> False

Since in the first case the ("hello" + nothing) gets garbage
collected, CPython is allowed to re-use its id. If instead you assign
them outside of the expression:

nothing = "!"

x = "hello" + nothing
y = "hello" + nothing
id(x) == id(y)
#>>> False

the collection cannot happen.

Note that in this case CPython is allowed to deduplicate these strings
anyway (although in this case it does not), so using "is" here is not
safe.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: id == vs is

2014-10-26 Thread MRAB

On 2014-10-27 00:12, Dan Stromberg wrote:

Are the following two expressions the same?

x is y

Id(x) == id(y)

?


Yes.


I ported some Java code to Python, and it was using Java's idea of
equality (via ==) in some places.  Right now, I have a suite of unit
tests working using the second expression above, but I'm thinking
about switching to the first.

Thanks!



--
https://mail.python.org/mailman/listinfo/python-list