When is it a pointer (aka reference) - when is it a copy?

John Henry john106henry at hotmail.com
Wed Sep 13 16:10:01 EDT 2006


Thanks for the reply, both to Laszlo and Steve.

Okay, I understand what you're saying.

But what if I need to make a "pointer" to a simple variable.

For instance, in C:

   int i=1
   int *j=&i

   *j = 2
   print i

and you get 2 printed.

In Python,

   i=1
   j=i
   j=2
   print i

and you get 1 printed.

So, if I understand you correctly, I must make the reference to a more
elaborate representation.  Like:

   i=[1,]
   j=i
   j[0]=2
   print i

in order to get 2 printed.

Correct?


Steve Holden wrote:
> John Henry wrote:
> > Hi list,
> >
> > Just to make sure I understand this.
> >
> > Since there is no "pointer" type in Python, I like to know how I do
> > that.
> >
> > For instance, if I do:
> >
> >    ...some_huge_list is a huge list...
> >    some_huge_list[0]=1
> >    aref = some_huge_list
> >    aref[0]=0
> >    print some_huge_list[0]
> >
> > we know that the answere will be 0.  In this case, aref is really a
> > reference.
> >
> > But what if the right hand side is a simple variable (say an int)?  Can
> > I "reference" it somehow?  Should I assume that:
> >
> >    aref = _any_type_other_than_simple_one
> >
> > be a reference, and not a copy?
> >
> Yes. Attributes are always object references. The assignment is actually
> the binding of a specific object to a name in some namespace, (r to an
> element of a sequence or other container object.
>
> This applies *whatever* the type of the RHS experession: the expression
> is evaluated to yield an object, and a reference to the object is stored
> in the name or container element.
>
> regards
>   Steve
> --
> Steve Holden       +44 150 684 7255  +1 800 494 3119
> Holden Web LLC/Ltd          http://www.holdenweb.com
> Skype: holdenweb       http://holdenweb.blogspot.com
> Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list