0

I have written simple Python code

#For String Types
d_new = dict(a_str="captainmarvel", b_str="captainmarvel", c_str="captainmarvel", d_str="captainmarvel")
print(id(d_new['a_str']))
p_str = "captainmarvel"
print(id(p_str))

#For int types
d = dict(a=4, b=4, c=4, d=4)
print(id(d['a']))
p = 4
print(id(p)) 

it returns output as

270985349584
270985349584
1559653536
1559653536

which are same object ids for integer value 4 and string value "captainmarvel". So does python reuses/pools its basic data types e.g. int, string

glibdud
  • 7,550
  • 4
  • 27
  • 37
rishi007bansod
  • 1,283
  • 2
  • 19
  • 45

1 Answers1

1

The CPython implementation occasionally does this for immutable objects, yes. (Other implementations like PyPy, Jython, IronPython, etc. might not.)

When exactly it does it is an implementation detail; most of the time you don't really need to bother with object identity.

AKX
  • 152,115
  • 15
  • 115
  • 172