0
a = 2j
print(id(a))

In this, the id(a) returns different value each time I run the program. What is the reason behind this?

On the other hand,

a = 2
print(id(a))

That variation is not seen with the above code where a = 2 refers to an integer and not complex number.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Kaushik
  • 187
  • 3
  • 13
  • 1
    python maintains a cahce for some integers but it doesnt do it for complex numbers thats why the behaviour is different, check out https://stackoverflow.com/questions/15171695/whats-with-the-integer-cache-maintained-by-the-interpreter – Chris Doyle Dec 11 '19 at 10:45
  • 1
    Small ints are exceptional. Ids being different is the expected behaviour. – khelwood Dec 11 '19 at 10:45

0 Answers0