When is it a pointer (aka reference) - when is it a copy?
grante at visi.com
Wed Sep 13 22:46:31 CEST 2006
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
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,
The name "i" is bound to an immutable integer object who's value is 1.
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.
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
> So, if I understand you correctly, I must make the reference
> to a more elaborate representation. Like:
> print i
> in order to get 2 printed.
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
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.
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.
 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
More information about the Python-list