0

Consider the following:

x = 1
y = 1 
print hex(id(x))
print hex(id(y))

Output:

0x2198158
0x2198158

x and y are two different variable that happen to hold the same value. Why is python assigning the same memory location to these two?

Next let us consider strings:

x = 'a'
y = 'a'
print id(x) == id(y)

Output:

True
Nagesh Eranki
  • 125
  • 1
  • 9
  • 3
    That's not the `id` of the _variable_. It's the `id` of the object held by the variable. And it's the same object held by both variables. – khelwood Jun 27 '17 at 09:08
  • 1
    You may get detailed info of Python variables and Memory Management in http://foobarnbaz.com/2012/07/08/understanding-python-variables/ – CWLiu Jun 27 '17 at 09:20
  • The string version is a different question. It's explained here: https://stackoverflow.com/questions/15541404/python-string-interning – Håken Lid Jun 27 '17 at 10:19

0 Answers0