"always passes by reference"

Donn Cave donn at oz.net
Fri Jul 28 00:59:02 EDT 2000


Quoth David Goodger <dgoodger at bigfoot.com>:
| "Pass by Spanish Inquisition" is a highly Pythonic term, and one that I like
| a lot. However... (and forgive me if this has already been mentioned)
|
| I've heard Python's parameter passing termed "pass by assignment". So a
| function:
|
|     def f(x):
|         pass
|
| which is called as:
|
|     f(y)
|
| simply does the assignment:
|
|     x = y
|
| with x in the function's namespace, and y in the caller's namespace. So what
| happens in regards to values/references? Lists and dictionaries appear to be
| passed by reference, because they're mutable. Strings, tuples and numbers
| appear to be passed by value, because they're immutable. Not having delved
| into the internals of the process, I can't authoratively say, but this seems
| a perfectly reasonable explanation to me. All parameters are simply assigned
| or bound to new names in new local namespaces. What is possible depends on
| the type of object that's passed.

Sounds OK to me, except I still say, mutable really has nothing to
do with it.  For sure, if you want to pass a parameter and see it
change, it will have to be mutable.  But the passing mechanism is the
same.  The semantics of the "variable" that was passed is the same.
The properties of the object that was passed, like mutability, are
not involved in the parameter passing mechanism.

	Donn Cave, donn at oz.net



More information about the Python-list mailing list