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

John Henry john106henry at hotmail.com
Wed Sep 13 22:56:57 CEST 2006


Thanks for the reply, Grant.

I am not doing things like that - I am just trying to clear up in my
mind the Python concepts.

I understand it now.



Grant Edwards wrote:
> On 2006-09-13, John Henry <john106henry at hotmail.com> wrote:
> > 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.
>
> There's no such thing as a "simple variable".  There are
> mutable objects and immutable objects.  Names are bound to
> objects.
>
>   x = 3
>
> The name "x" is bound to an immutable integer object who's
> value is 3.
>
> > For instance, in C:
> >
> >    int i=1
> >    int *j=&i
> >
> >    *j = 2
> >    print i
> >
> > and you get 2 printed.
> >
> > In Python,
> >
> >    i=1
>
> The name "i" is bound to an immutable integer object who's value is 1.
>
> >    j=i
>
> The name "j" is bound to an immutable integer object who's
> value is 1. That may or may not be the same object to which
> "i" is bound.
>
> >    j=2
>
> Now the name "j" is bound to an immutable integer object who's
> value is 2.  Rebinding the name "j" to a different object has
> no effect on the object to which "i" is bound.
>
> >    print i
> >
> > and you get 1 printed.
>
> Because you've changed neither the object to which "i" is bound
> nor the value of that object (you can't change the values of
> integer objects).
>
> > 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?
>
> I suppose, for some values of "correct".  You've bound the
> names "i" and "j" to the same mutable object, then mutated that
> object.  Afterwards "i" and "i" still refer to that mutated
> object.
>
> That'll work as a rather clumsy imitation of the C code, but I
> don't really see what it is you're trying to accomplish. Trying
> to write C code using Python isn't going to be fun or productive[1].
>
> When using Python, you should write Python code. ;)
>
> If you'll explain the actual problem you're trying solve for
> which you think you need C-style "pointers", then somebody will
> be happy to show you how that problem is solved using Python.
>
> [1] There are people here who probably think it fun, but only
>     as a brain-teaser.
>
> --
> Grant Edwards                   grante             Yow!  After THIS, let's go
>                                   at               to PHILADELPHIA and have
>                                visi.com            TRIPLETS!!




More information about the Python-list mailing list