Finding the instance reference of an object

gooberts at gmail.com gooberts at gmail.com
Mon Oct 27 14:19:41 EDT 2008


On Oct 17, 5:39 pm, Joe Strout <j... at strout.net> wrote:
> On Oct 17, 2008, at 3:19 PM, Grant Edwards wrote:
>
> >> And my real point is that this is exactly the same as in every
> >> other modern language.
>
> > No, it isn't.  In many other languages (C, Pascal, etc.), a
> > "variable" is commonly thought of as a fixed location in memory
> > into which one can put values.  Those values may be references
> > to objects.
>
> Right, though not in languages like C and Pascal that don't HAVE the  
> notion of objects.  We really ought to stop bringing up those  
> dinosaurs and instead compare Python to any modern OOP language.
>
> >  In Python, that's not how it works.  There is no
> > "location in memory" that corresponds to a variable with a
> > particular name the way there is in C or Pascal or Fortran or
> > many other languages.
>
> No?  Is there any way to prove that, without delving into the Python  
> source itself?
>
> If not, then I think you're talking about an internal implementation  
> detail.


I think this "uncontrived" example addresses the C/Python difference
fairly directly (both were tested):
----------------------------------
C:

struct {int a;} s1, s2;

int main()
{
   s1.a = 1;
   s2 = s1;

   printf("s1.a %d   s2.a %d\n", s1.a, s2.a);
   s1.a = 99;
   printf("s1.a %d   s2.a %d\n", s1.a, s2.a);
}

----------------------------------
Python:

class mystruct:
    pass

s1 = mystruct()
s1.a = 1
s2 = s1

print "s1.a %2d   s2.a %2d" % (s1.a,s2.a)
s1.a = 99
print "s1.a %2d   s2.a %2d" % (s1.a,s2.a)

---------------
C OUTPUT:
  s1.a  1   s2.a  1
  s1.a 99   s2.a  1

Python OUTPUT:
  s1.a  1   s2.a  1
  s1.a 99   s2.a 99

Note that in C (or C++) the value of s2.a remains unchanged, because
the VALUE of s1 (the contents of the memory where s1 resides) was
COPIED to the memory location of s2, and subsequently, only the VALUE
of s2.a was changed. In Python, s2.a is "changed" (but not really)
because it turns out that s2 is just another name for the object that
s1 pointed to.

So there is no programatically accessible "location in memory" that
corresponds to s1 or s2 in Python. There is only a location in memory
that corresponds to the object that s1 is currently pointing to. In C,
by contrast, there are definite locations in memory that correspond to
both variables s1 and s2, and those locations remain always separate,
distinct and unchanged throughout the execution of the program.

This is not an "implementation detail", it is a fundamental part of
each language. As C was an "improvement" on assembler, the variable
names have always just been aliases for memory locations (or
registers). You can see this in the output of any C/C++ compiler.

In Python, variables are just names/aliases for *references* to
objects, not names for the objects/values themselves. The variable
names themselves do not correspond directly to the objects' memory
locations. While yes, technically, it is true that those reference
values must be stored somewhere in memory, *that* is the
implementation detail. But is is not the *locations* of these
references (i.e., the locations of the Python *variables*) that are
copied around, it is the references themselves (the locations of the
Python *objects*) that are copied.

> > All that exists in Python is a name->object mapping.
>
> And what does that name->object mapping consist of?  At some level,  
> there has to be a memory location that stores the reference to the  
> object, right?

I think this is answered above, but just to drive it home, in Python
the memory locations of the variables themselves (an implementation
detail), which hold the references to the objects, are inaccessible .
In C/C++, by contrast, variable names correspond directly to memory
locations and objects, and you can easily find the addresses of
variables.

In C/C++, if you choose, you may have a variable that is itself a
reference/pointer to some other memory/object. In C, we would say that
the VALUE of that variable is the memory address of another object.
But you can, if you need to, get the address of the pointer variable,
which points to the *address* of the other object.

In Python, a variable is ONLY EVER a reference to an object. You
cannot get the address of a Python variable, only of a Python object.

Hope this clears things up.

dale



More information about the Python-list mailing list