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?
"==" operator compares by checking for equality
"is" operator compares identities
(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.
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)
(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
.
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.