Passing arguments to function - (The fundamentals are confusing me)

Rocco Moretti roccomoretti at hotpop.com
Tue Aug 9 13:12:26 EDT 2005


Gregory Piñero wrote:
> Ahh, so it's a mutable thing.  That makes sense that I can't change a
> mutable object and thus can't affect it outside of the function.  

If you meant "immutable" for the second mutable, you're right.

> Does
> that mean Python functions aren't always byref, but are sometimes
> byval for nonmutables?

It'd probably do you good to get away from the by reference/by value 
thinking. Python isn't C/Basic/Fortran/etc.

Variables in Python are names. They aren't the cubbyholes into which you 
put values, they are sticky notes on the front of the cubby hole.

Parameter passing in Python always work the same way - you create a new 
name pointing to the passed object. Fin.

The confusion you're having isn't in parameter passing, it's in the 
difference between assignment and mutation. Mutation changes the object 
itself (what's in the cubby hole), so it doesn't matter what or how many 
names/variables it has (what sticky notes are on the front). Assigment 
just changes where names point, not the contents of objects. (It's 
moving that sticky note, and only that sticky note, from one cubby to a 
different one.) Assignment justs affects that name, not any other name 
which point to the same object, including the variables in the passing 
scope.



More information about the Python-list mailing list