-1

in my institute i was told that every variable in python has it's own memory address and memory address in each variable is different.They proved it by using id() command , they showed us

>>>a=10
>>>b=20
>>>id(a)
94349304104992
>>>id(b)
94349304105312

by showing this statement they proved us how both the memory location are different.

then i was trying the same thing with my linux terminal and got some weird answer when i tried....

>>>id(999)
139978570234704
>>>id(10001) 
139978570234704
>>>id(9999)
139978570234704
>>>id(100001)
139978570234704
>>>id(99999)
139978570234704
>>>id(1000001)
139978570234704
>>>id(999999)
139978570234704
>>>id(10000001)
139978570234704

after trying all this i found that this all numbers i pointing toward same location and i can not understand why this is happening only with this kind of number

beside this when i tried the same thing on windows cmd it worked fine. Every values got allocated to the different location

  • 1
    Please first explain why you think it should work differently. – Karl Knechtel Oct 04 '20 at 03:52
  • 1
    Does this answer your question? [Unnamed Python objects have the same id](https://stackoverflow.com/questions/24802740/unnamed-python-objects-have-the-same-id) and [Why is the id of a Python class not unique when called quickly?](https://stackoverflow.com/q/20753364/4518341) – wjandrea Oct 04 '20 at 04:16

1 Answers1

0

See the documentation:

id(object)

Return the "identity" of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

CPython implementation detail: This is the address of the object in memory.

wjandrea
  • 28,235
  • 9
  • 60
  • 81