Why does the 'is' operator behave unexpectedly with arithmetically equal expressions

frost101

After reading this and this, I still cannot understand the following behaviour:

a = 1000
b = 1000
print (a == b)
print (a is b)
print (f"id(a) = {id(a)} \nid(b) = {id(b)}")

As expected I get

True
True
id(a) = 2806705928816 
id(b) = 2806705928816

But when i try to do something like this:

a = 1000
b = 1000 + a - a
print (a == b)
print (a is b)
print (f"id(a) = {id(a)} \nid(b) = {id(b)}")

I got False in expression a is b

True
False
id(a) = 3030783801968 
id(b) = 3030783802064

Why does a variable behave differently when assigning the result of an expression over a integer and an expression with other variables to it? Although mathematically this gives the same integer.

Rishabh Kumar

When you do something like :

(case-1)

a = 1000
b = a

or (case-2)

a = 1000
b = 1000

Python is smart enough to know before hand that even after execution you won't need new memory.

So, python just before execution makes b an alias of a in the first case.

The second case is bit different. Python is a true object oriented language, the literal 1000 is treated as an object. (Intuitively you can think as 1000 to be name of a const object).

So in second case a and b are technically, both becoming alias of 1000

Now in your example:

a = 1000
b = 1000 + a - a
print (a == b)
print (a is b)

while assignment of b, python doesn't know before hand what is going to be the value of a. When I say before-hand I mean before any form of calculation being started. So python reserves a new memory location for band then saves the output of the operation in this new memory location.

It is also worth noting this:

4-1 is 3
True

In this case, python doesn't saves this line with 4-1 but processes it before compilation to be 3, for runtime optimisation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does the 'is' operator behave unexpectedly with arithmetically equal expressions

From Dev

Nested generator expressions behave unexpectedly

From Dev

Why does '+' operator behave abnormally in JavaScript?

From Dev

Why does the null-conditional operator behave differently for == and .Equals()?

From Dev

Why does the JavaScript "delete" operator behave differently in different browsers?

From Dev

Why does the pre/post increment operator behave wrongly?

From Dev

Why does the JavaScript "delete" operator behave differently in different browsers?

From Dev

Why does this use of wxPython's EVT_KILL_FOCUS behave unexpectedly?

From Dev

why ng-model behave unexpectedly in angularjs?

From Dev

Why does the is operator say two equal ids are not the same?

From Java

Why does \R behave differently in regular expressions between Java 8 and Java 9?

From Dev

Why does \R behave differently in regular expressions between Java 8 and Java 9?

From Dev

Why do these generator expressions behave differently?

From Dev

Why those two expressions are not equal?

From Dev

Why do my program behave unexpectedly when I use sigaction?

From Dev

Why does this promise not behave as expected?

From Dev

Why does this promise not behave as expected?

From Dev

Why does this queue behave like this?

From Dev

Why does this code behave inconsistently?

From Dev

How does addition assignment operator behave

From Dev

Why does "cp -r a/. b" behave as it does?

From Dev

Why does the template-id in "A<0>=0" not compile without space because of the greater-or-equal-than operator ">="?

From Dev

Why does my private variable equal zero when I overload the array operator?

From Dev

Why do Java regular expressions behave differently on Linux and Windows?

From Dev

Why does const_cast not behave as expected?

From Dev

Why does a function behave like a computed?

From Dev

Why does strptime() behave differently on OSX and on Linux?

From Dev

Why does this array initialize behave differently?

From Dev

Why does this constraint behave differently on different simulators?

Related Related

  1. 1

    Why does the 'is' operator behave unexpectedly with arithmetically equal expressions

  2. 2

    Nested generator expressions behave unexpectedly

  3. 3

    Why does '+' operator behave abnormally in JavaScript?

  4. 4

    Why does the null-conditional operator behave differently for == and .Equals()?

  5. 5

    Why does the JavaScript "delete" operator behave differently in different browsers?

  6. 6

    Why does the pre/post increment operator behave wrongly?

  7. 7

    Why does the JavaScript "delete" operator behave differently in different browsers?

  8. 8

    Why does this use of wxPython's EVT_KILL_FOCUS behave unexpectedly?

  9. 9

    why ng-model behave unexpectedly in angularjs?

  10. 10

    Why does the is operator say two equal ids are not the same?

  11. 11

    Why does \R behave differently in regular expressions between Java 8 and Java 9?

  12. 12

    Why does \R behave differently in regular expressions between Java 8 and Java 9?

  13. 13

    Why do these generator expressions behave differently?

  14. 14

    Why those two expressions are not equal?

  15. 15

    Why do my program behave unexpectedly when I use sigaction?

  16. 16

    Why does this promise not behave as expected?

  17. 17

    Why does this promise not behave as expected?

  18. 18

    Why does this queue behave like this?

  19. 19

    Why does this code behave inconsistently?

  20. 20

    How does addition assignment operator behave

  21. 21

    Why does "cp -r a/. b" behave as it does?

  22. 22

    Why does the template-id in "A<0>=0" not compile without space because of the greater-or-equal-than operator ">="?

  23. 23

    Why does my private variable equal zero when I overload the array operator?

  24. 24

    Why do Java regular expressions behave differently on Linux and Windows?

  25. 25

    Why does const_cast not behave as expected?

  26. 26

    Why does a function behave like a computed?

  27. 27

    Why does strptime() behave differently on OSX and on Linux?

  28. 28

    Why does this array initialize behave differently?

  29. 29

    Why does this constraint behave differently on different simulators?

HotTag

Archive