Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why variables holding same value has the common ID in Python? [duplicate]

Tags:

python

In Python:

>>> a = "Hello"
>>> b = "Hello"
>>> id(a) == id(b)
True

Why? Can this create a problem in a complex programs which refer to the memory locations of the object?

like image 449
manas lohar Avatar asked Oct 29 '25 06:10

manas lohar


1 Answers

From the Python documentation

For immutable types [like strings], operations that compute new values may actually return a reference to any existing object with the same type and value. E.g., after a = 1; b = 1, a and b may or may not refer to the same object with the value one, depending on the implementation...

like image 76
xiº Avatar answered Oct 31 '25 20:10

xiº



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!