"always passes by reference"

David Goodger dgoodger at bigfoot.com
Thu Jul 27 22:49:58 EDT 2000


"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.

Of course, this just passes the buck over to, "so how is assignment
performed in Python?". But that seems to be an easier question to answer.

-- 
David Goodger    dgoodger at bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come!)




More information about the Python-list mailing list