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:

4
  • 1
    This is not "object comparison". This is checking whether a and b refer to the same object in memory. Commented May 14, 2020 at 14:02
  • 1
    Possible duplicates: stackoverflow.com/questions/53441411/… stackoverflow.com/questions/54071180/… Commented May 14, 2020 at 14:08
  • Hi Tharmila, welcome to SO! Please read up on writing questions before asking your next one. Happy coding! Commented May 14, 2020 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. Commented May 14, 2020 at 14:16

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

8 Comments

This doesn't explain why the results in the console and the script are different, though
But, why it's giving different output for a same code?(in script and while running a file)
@ForceBru it is updated now. I will try to make it even more clear tho
@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?
The data model is described here: docs.python.org/3.5/reference/datamodel.html# it states that each block has it's own copy of an object.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.