1

I have tried the same code in python, once ran as a .py file and once typed in IDLE, but it gives different output for the same code:

a = 3.4
b = 3.4
a is b

I have attached a screenshot taken while trying by both methods:

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Meera
  • 11
  • 3
  • 1
    This is not "object comparison". This is checking whether `a` and `b` refer to _the same object in memory_. – ForceBru May 14 '20 at 14:02
  • 1
    Possible duplicates: https://stackoverflow.com/questions/53441411/python-interactive-terminal-results-are-inconsistent-with-script-results https://stackoverflow.com/questions/54071180/an-irregular-anomaly-in-python-tuple/54071740#54071740 – ForceBru May 14 '20 at 14:08
  • Hi Tharmila, welcome to SO! Please read up on [writing questions](https://stackoverflow.com/help/how-to-ask) before asking your next one. Happy coding! – Diggy. May 14 '20 at 14:09
  • Could you debug this for me please. On boths sides please do: `print(id(a)) print(id(b))` this will return the id of the internal object the variable is pointing to. – Gertjan Brouwer May 14 '20 at 14:16

1 Answers1

3

The reason for why your left window returns false and the right true is because of each command you type is a block, as stated in the manual:

A Python program is constructed from code blocks. A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block.

Thus when you are using your console, each individual command you type is considered a block. Each block has constants which are reused. In your case 3.4 is a constant. But when you are typing the second command, it is considered a new block so it won't find a constant which it can reuse. In the second case of using a .py file the constant is saved and reused, because a file is seen as a single code block.

A way for you to check this is to declare both variables on the same line like this:

>>> a = 3.4; b = 3.4;
>>> print(a is b)

This will output True, because you declare both variables in the same command, thus block.

If you however are trying to just compare two variables you should use the ==. Keep in mind that you are doing floating point comparison, check out this stackoverflow on how to best do that: floating point comparison in Python

Gertjan Brouwer
  • 996
  • 1
  • 12
  • 35
  • This doesn't explain why the results in the console and the script are different, though – ForceBru May 14 '20 at 14:11
  • But, why it's giving different output for a same code?(in script and while running a file) – Meera May 14 '20 at 14:14
  • @ForceBru it is updated now. I will try to make it even more clear tho – Gertjan Brouwer May 14 '20 at 14:24
  • @GertjanBrouwer, hmmm, that's an interesting approach - I haven't seen an explanation of this phenomenon that uses blocks. Is there any information about whether each block must have its unique copy of an object, like the `3.4`? – ForceBru May 14 '20 at 14:32
  • The data model is described here: https://docs.python.org/3.5/reference/datamodel.html# it states that each block has it's own copy of an object. – Gertjan Brouwer May 14 '20 at 14:46
  • While trying same thing for integer it's giving correct output even without using the semicolon.how it's possible? – Meera May 16 '20 at 06:01
  • @Tharmila this behaviour, as far as I know, should be the same for integers and floats. Can you show me what you have done? – Gertjan Brouwer May 16 '20 at 11:53
  • When I tried a=2 and next line b=2 and if I print a is b it gives true...but for floating point it gives false if I tried this way. So without using semicolon for integers it's giving correct out. – Meera May 16 '20 at 12:51