By value or by reference?

Josiah Carlson jcarlson at uci.edu
Mon Oct 18 16:46:28 EDT 2004


> The key point is that, in both languages, a function's arguments are
> just like local variables pre-initialized by the caller with assignment
> statements.  In both languages, if the function reassigns an argument
> it's just playing with its own local variables, the caller is never in
> any way affected by that.  In both languages, there are other ways
> except plain barename assignments to possibly "affect the caller" (in C
> you basically need to have been passed a pointer, and dereference that
> pointer or another pointer computed from the first by pointer
> arithmetic; in Python you basically need to have been passed a mutable
> object, and call mutating methods on that object).  Again, in both cases
> I see simplicity and consistency, everything pretty explicit...

I agree with most everything you have said, though consider the pointer
vs. value of C to define the semantics of the passing.  That is, if you
get a pointer in C, it is by reference.  If you don't get a pointer, it
is by value.

I also agree that everything is pure and explicit in both languages, it
took me a few minutes mucking around with the interpreter my first time
to understand how Python deals with the base types.

I'm trying to point out that if you toss the standard C semantic
definition of "pass by value" and "pass by reference", by merely
pretending that the definitions have not been given in the history of
computer science, and just look at how Python does actual name binding
in namespaces/scopes, you can understand what Python does much better
than to get into a "Python does pass by reference" vs. "Python does pass
by value" argument.


 - Josiah




More information about the Python-list mailing list