Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how does "is" work ?
#1
I met a strange problem when using is. Here is the code:
>> i = "12"
>> j = "12"
>> i is j
True
>> i = "1 2"
>> j = "1 2"
>> i is j
False
When I move the code to a file, the output will be True True. What happened?
Reply
#2
"==" operator compares by checking for equality

"is" operator compares identities
Reply
#3
(Sep-08-2019, 05:29 AM)Evil_Patrick Wrote: "==" operator compares by checking for equality "is" operator compares identities
I know the definition of is. Here I want to know why is returns different results with different strings("12" and "1 2"), and why the results differ in IDE and file.
Reply
#4
It's always good to start with built-in help (>>> help('is'). You can verify whether your belief that you know definition of 'is' is truthy:

From aforementioned help:

Quote:Identity comparisons
====================

The operators "is" and "is not" test for object identity: "x is y" is
true if and only if *x* and *y* are the same object. Object identity
is determined using the "id()" function. "x is not y" yields the
inverse truth value.

Also I don't understand the claim: 'When I move the code to a file, the output will be True True.' This code doesn't have any output if you run it from file (no print statement)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Sep-08-2019, 09:14 AM)perfringo Wrote: It's always good to start with built-in help (>>> help('is'). You can verify whether your belief that you know definition of 'is' is truthy: From aforementioned help:
Quote: Identity comparisons ==================== The operators "is" and "is not" test for object identity: "x is y" is true if and only if *x* and *y* are the same object. Object identity is determined using the "id()" function. "x is not y" yields the inverse truth value.
Also I don't understand the claim: 'When I move the code to a file, the output will be True True.' This code doesn't have any output if you run it from file (no print statement)
Sorry, I should make my problem clear.
in python commandline, I define two same strings and use is to identify whether they are the same object. Just like you see above, sometimes they are (like 12), Sometimes not (like 1 2). But when I move all the code to a file, they are always the same object.
I want to know when strings are the same object and why result differs in commandline and file ? Here is the code in test file:
i = "12"
j = "12"
print(i is j)
i = "1 2"
j = "1 2"
print(i is j)
The output is True True, while in commandline, it's True False.
Reply
#6
from the docs

Quote:Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after a = 1; b = 1, a and b may or may not refer to the same object with the value one, depending on the implementation, but after c = []; d = [], c and d are guaranteed to refer to two different, unique, newly created empty lists. (Note that c = d = [] assigns the same object to both c and d.)

in your example you use str (i.e. immutable type) so there is no guarantee both variable will point to same object.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020