3

I believe everything is an object in Python but I saw weird behaviour.

Here is a snippet from Python 3.6.5

a = 300
b = 300
a is b

Out[4]: False

a = 200
b = 200
a is b

Out[7]: True

Can someone please explain this?

ronilp
  • 455
  • 2
  • 9
  • 25
  • In python integers are inmutable, and it is up to him to reuse the same addresses for diferent variables. In the first case it allocates 2 numbers, in the second just one. – Netwave Mar 11 '19 at 04:16
  • 1
    @Netwave that's why metatoaster mentioned the other question. In Python 2 and 3, only low-value integers from -5 to 256 are pre-allocated in the memory, other integers will be calculated at run-time. https://stackoverflow.com/a/306353/4055552 https://docs.python.org/2/c-api/int.html – Luan Nguyen Mar 11 '19 at 04:28

0 Answers0