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

Christophe chris.cavalaria at free.fr
Thu Sep 14 07:54:41 EDT 2006


John Henry a écrit :
> 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?
> 
> Thanks,
> 

That's easy. In Python, every variable is a depth one pointer. Every 
variable is of the type (PyObject*)

Of course, since numbers and strings are immutable, that pointer is 
useless to push back the modifications you've done.

You need a PyObject** ? Use a one element list instead and manipulate it 
like that : number_ref[0] = new_value
instead of that : number_ref = [new_value]




More information about the Python-list mailing list