Reputation: 453
I notice that, for integer and string:
a=1 ; b=1
c='abc' ; d='abc'
print(id(a), id(b))
print(id(c), id(d))
In this circumstance, a
would share same memory address with b
, c
would share same memory address with d
On the other hand, for list, set ...etc.:
e=[1,2,3] ; f=[1,2,3]
g=(1,2,3) ; h=(1,2,3)
i={1,2,3} ; j={1,2,3}
k={"a":1,"b":2} ; l={"a":1,"b":2}
print(id(e), id(f))
print(id(g), id(h))
print(id(i), id(j))
print(id(k), id(l))
In this circumstance, e
won't share same memory address with f
, g
won't share same memory address with h
... etc.
So I want to ask:
Thank you
Upvotes: 0
Views: 561
Reputation: 21
The python Interpreter has different way of handling this concept. The above concept you test in VScode or any other editor then x = 12345678 y = 12345678
print(x is y) This will return in True as result Click here to see the concept
Upvotes: 2
Reputation: 2737
Only a partial answer specific to integers: Because of the frequency of use, Python caches then reuses certain integer values in memory. For example, the numbers 1-256 are stored that way which is why different variables pointing at those values will have the same id. Larger numbers, for example 257, are dynamically allocated and will not share the same id despite being immutable.
Example:
a = 256
b = 256
print(id(a), id(b))
a = 257
b = 257
print(id(a), id(b))
Output:
2038199456 2038199456
21831936 21832816
Upvotes: 2