[Tutor] does id function return location of reference or the referenced object?

Serdar Tumgoren zstumgoren at gmail.com
Fri Mar 6 17:08:48 CET 2009


Hi all,

I've managed to think myself in circles and was hoping someone could help
clarify the nature of the "id" function and references in general.

I initially got confused while dealing with file objects, so I'll stick with
that example.  My question, based on the below tests in the ipython
interpreter, is whether the id function is returning the location of the
file object or the reference to that object (in this case, "f"). My hunch is
that it's the memory location of the file object, but then I started
thinking that everything in python is an object, so shouldn't there be a
memory location for the variable name "f" as well? And are the below memory
locations for that reference rather than the object itself?

The examples below suggest (to me) that the memory address is for the file
object itself.  But I'm not familiar enough with python's internals to know
if I'm misinterpreting the results and generally muddling the concepts.

Any advice is greatly appreciated.

Regards,
Serdar

<<<begin ipython interpreter example>>>

In [26] f = open('class_test.py')

In [27]: f
Out[27]: <open file 'class_test.py', mode 'r' at 0x84a5b18>

In [28]: id(f)
Out[28]: 139090712

In [29]: type(f)
Out[29]: <type 'file'>

In [30]: 0x84a5b18
Out[30]: 139090712

<<<end example>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090306/50462431/attachment.htm>


More information about the Tutor mailing list