5

I am exploring python is vs ==, when I was exploring it, I find out if I write following;

>>> a = 10.24
>>> b = 10.24

in a python shell and on typing >> a is b, it gives me output as false. But when I write the following code in a python editor and run it I get true.

a = 10.24
b = 10.24
print(a is b)

Can anyone explain why I am getting two different results of the same variables and expression?

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • 2
    These are entirely different situations; in the shell, each line you type is compiled separately, but a script is compiled all at once - allowing the compiler to notice that you've used the same constant twice. Note that neither behavior is actually guaranteed by the language, if you are relying on it then your code is broken. – jasonharper Jul 20 '21 at 13:22
  • @jasonharper python is not a compiled language so I do not really understand your answer... Could you explain further what you mean? This is quite strange since the same experiment done with integers (e.g.: 10) gives True in both shell and a file script – Matteo Zanoni Jul 20 '21 at 13:29
  • 1
    @MatteoZanoni see my answer for an explanation of why that happens. Python caches integers between [-5,256] as singletons, so any integer in that range will return true for an `a is b` statement. Also see [What's with the integer cache maintained by the interpreter?](https://stackoverflow.com/a/15172182/5763413) – blackbrandt Jul 20 '21 at 13:38
  • 'is' has two intended uses. 1. Make comparisons in those uncommon cases where '==' is not specific enough. The most common of these are `x is None` and `x is not None`. 2. Test implementation-specific internal details. The Python test suite has some, which are marked as CPython-only tests. There is one for CPython-specific caching of integers. – Terry Jan Reedy Jul 21 '21 at 04:14

2 Answers2

5

You should not rely on is for comparison of values when you want to test equality.

The is keyword compares id's of the variables, and checks if they are the same object. This will only work for the range of integers [-5,256] in Python, as these are singletons (these values are cached and referred to, instead of having a value stored in memory). See What's with the integer cache maintained by the interpreter? This is not the same as checking if they are the same value.

As for why it behaves differently in a REPL environment versus a passed script, see Different behavior in python script and python idle?. The jist of it is that a passed script parses the entire file first, while a REPL environment like ipython or an IDLE shell reads lines one at a time. a=10.24 and b=10.24 are executed in different contexts, so the shell doesn't know that they should be the same value.

blackbrandt
  • 2,010
  • 1
  • 15
  • 32
  • 2
    Very helpful explanation. This is the typical strange behaviour that will make you understand the inner workings of python a little more. I've definitely learned something today – Matteo Zanoni Jul 20 '21 at 13:49
0

In python editor

a = 10.24
b = 10.24
print(id(a),id(b))

output -

2079062604112 2079062604112

If we relate this to C, then 2079062604112, 2079062604112 are actually the memory address, here in Python it is the unique id,which are same in python editor.

In shell-

>>> a = 10.24
>>> b = 10.24
>>> id(a)
2254043146288 # output
>>> id(b)
2254043400016 # output

Giving different unique id.
So when is used for comparison it compares the unique id's that's why u are getting different answers.
Hope you finds this helpful.

  • 1
    Without a doubt is keyword checks for the address. But why it is behaving differently in shell and editor – Furkhan Shaikh Jul 20 '21 at 17:40
  • https://towardsdatascience.com/python-memory-and-objects-e7bec4a2845 this article will clear all your doubts regarding `is`,`==`and `basics of Python and memory management`, what I found in here is python is uses the process called 'Interning' to optimize memory allocation that is the root cause of your question. – dhananjay sawarkar Jul 20 '21 at 18:50
  • I read that article. I do understand interning concept. But still my question is why interning is behaving differently on shell and editor. Same statement have 2 different answers, on shell and editor – Furkhan Shaikh Jul 21 '21 at 01:09