1

I was just playing around with the id() function & noticed that if you pass a value that you haven't assigned to a variable yet, instead of giving an error it returns a memory address. So does the id() function assign that value to an empty memory location?

>>> print id(18)
14569728
>>> b = 18
>>> print id(b)
14569728
>>> print id('check')
140297315282112
>>> c = 'check'
>>> print id(c)
140297315282112
elssar
  • 5,651
  • 7
  • 46
  • 71

1 Answers1

5

Values/literals are objects, so id() will always return a valid result for them. Beyond that, in CPython integer literals from -1 through 256 as well as short string literals are interned, so they will have a constant ID after they have been accessed for the first time.

Odomontois
  • 15,918
  • 2
  • 36
  • 71
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358