"always passes by reference"

Huaiyu Zhu hzhu at users.sourceforge.net
Fri Jul 28 15:00:28 EDT 2000


Let's see if this description make sense.

In python there are objects and there are names.  There are operations that
act on existing objects and those that create new objects.  These have
nothing to do with mutability, except that for immutable objects, there are
no operations acting on existing objects.

Assignments change what a name refers to:

a = b      # these two names now refer to the same object.
b = c      # b refers to the one c refers to, instead of the one a refers to.

Function calls pass reference of existing objects to local names in the
function.  Whether within the function you do things to the existing objects
or newly created objects is another matter:

b.method_call()        # A reference to the object refered by b (and c) is
                       # passed in (as self).
                       # The object may be modified in function.
                       # The names b and c still refer to the same object.

do_something_to(b)     # A reference to the object is passed in.
                       # The object may be modified in function.
		       # The names b and c still refer to the same object.

b = some_operation(b)  # A reference to the object is passed in.
                       # The object may be modified in function.
		       # The name c still refers to the same object.
		       # The assignment causes b to refer to the returned
		       # object, which may be the same, or different. 

The difficulty of new users from other languages is that they are not used
to regarding a name and the object it refers to as separate things. In
staticly typed languages, the association between names and objects are much
tighter - it happens at declarations and often never changes.

IMHO, the name/object dichotomy is much more descriptive for python than the
reference/value dichotomy.

Huaiyu


On Thu, 27 Jul 2000 22:49:58 -0400, David Goodger <dgoodger at bigfoot.com> wrote:
>"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.
>




More information about the Python-list mailing list