+ 2

Why does this return same memory address?

a = 5 b = 5 print(id(a)) print(id(b)) print(id(5))

10th Apr 2020, 7:00 AM
Anurag Mondal
Anurag Mondal - avatar
2 Answers
+ 6
Both variables a and b reference to the same object of class int - 5. Python cashes small integers to improve performance and memory consumption. To see this you can run the next code: import sys a = 5 print(sys.getrefcount(5)) # some count b = 5 print(sys.getrefcount(5)) # count + 1
10th Apr 2020, 8:17 AM
andriy kan
andriy kan - avatar
10th Apr 2020, 8:15 AM
Jayakrishna 🇼🇳