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

Kent Johnson kent37 at tds.net
Fri Mar 6 17:36:24 CET 2009

On Fri, Mar 6, 2009 at 11:08 AM, Serdar Tumgoren <zstumgoren at gmail.com> wrote:
> 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?

In CPython, at least, the id is the memory location of the object.
Variables are generally stored as dictionary entries. The string 'f'
does have an address, but that is not what id(f) refers to. Python
doesn't make any guarantees about this, it is an implementation
detail. The docs for id() say,

Return the ``identity'' of an object. This is an integer (or long
integer) which is guaranteed to be unique and constant for this object
during its lifetime. Two objects with non-overlapping lifetimes may
have the same id() value. (Implementation note: this is the address of
the object.)


More information about the Tutor mailing list