Memory Addresses: How Python Keeps Track of Values

Back in Values, Variables, and Computer Memory, you learned that Python keeps track of each value in a separate object and that each object has a memory address. You can discover the actual memory address of an object using built-in function id:

 >>>​​ ​​help(id)
 Help on built-in function id in module builtins:
 
 id(obj, /)
  Return the identity of an object.
 
  This is guaranteed to be unique among simultaneously existing objects.
  (CPython uses the object's memory address.)

How cool is that? Let’s try it:

 >>>​​ ​​id(-9)
 4301189552
 >>>​​ ​​id(23.1)
 4298223160
 >>>​​ ​​shoe_size​​ ​​=​​ ​​8.5
 >>>​​ ​​id(shoe_size)
 4298223112
 >>>​​ ​​fahrenheit​​ ​​=​​ ​​77.7
 >>>​​ ​​id(fahrenheit)
 4298223064

The addresses you get will probably be different from what’s listed here since values get stored wherever there happens to be free space. Function objects also have memory addresses:

 >>>​​ ​​id(abs)
 4297868712
 >>>​​ ​​id(round)
 4297871160
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.138.202