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