[Tutor] pointers for python?

Kristoffer Erlandsson krier115 at student.liu.se
Tue Oct 7 04:50:11 EDT 2003


On Tue, Oct 07, 2003 at 09:24:54PM +1300, Thomi Richards wrote:
[snip]
> AFAIK (And I'm no expert), in C, whenever a function is called, a *copy* of 
> the variables passed to it are made, and the function uses that copy, like 
> this:
> 
> void test(int a)
> {
> 	a++;
> 	printf("Integer value + 1 is: %d\n",a);
> }
> 
> if you were to call this like so:
> 
> int t = 42;
> test(t);
> 
> this would print 43, but the variable "t" (in the main scope of the program) 
> would remain 42, because the function test() was operating on a copy of the 
> variable, not the variable itself. is this basically correct?
> 
> However, when you use pointers in C, you are passing an address in memory, 
> which can be used like a variable. However, because you are passing the 
> original address, the function operates on the original varibale, not a copy 
> (this can of course be useful when you're dealing with larger structures, as 
> it saves the program chewing memory and CPU cycles to copy something which 
> you were effectively going to change anyway).
> 
> ...which brings us back to python. Evidently, python doesn't do this, with 
> *some* types...which is supreemly wierd.. IMO anyway...
> 
> 
> can someone offer a sensible explination as to why this happens?

I think the Python reference manual says things better than if I would
try to explain them :). First some things about object identity and
such: http://www.python.org/doc/current/ref/objects.html. And then onto
the type hierarchy which lists which types are immutable and mutable
among other things: http://www.python.org/doc/current/ref/types.html.

Hope this helps.

Regards,

-- 
Kristoffer Erlandsson                               http://errl.info



More information about the Tutor mailing list