Pointers

Martijn Faassen m.faassen at vet.uu.nl
Mon Mar 20 15:55:42 EST 2000


D-Man <dsh8290 at osfmail.rit.edu> wrote:
> Would I be correct if I said that Python always uses pointers and never
> makes a copy of an object unless explicitly requested using copy()?

Yes, it always uses reference semantics, never value semantics.

The neat thing is that immutable objects (such as integers, strings
and tuples (with only immutables inside)) behave the same under reference
semantics as under value semantics. Because the objects that you're
referring to don't change, it doesn't matter functionally whether they're
copied or if just new references are created.

So, Python automagically works in the optimal way; behavior indistinguishable
from pass-by-value for things small enough (in C, you *would* pass these
by value) and nice reference semantics for the larger stuff, where it
would cost to pass by value (and in C, you would need to go mess with
difficult pointers here ;).

Oh, and you can also explicitly copy a list by using [:].

Regards,

Martijn
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list