Simple newbie question about parameters handling in functions (or am I dump or what?)

Mike C. Fletcher mcfletch at rogers.com
Tue Feb 10 05:54:16 EST 2004


Niki Iv wrote:

>I am totaly new to Python, although I have quite strong developer
>background. Indeed I am evaluating Python to integrate it (Jython) in
>my Java apps... 
>Well this is my first Python day. I am tampering with functions. I
>read that parameters are passed by-value rather than by-reference. So
>function can change wathever parameters reffers to. My troube (well my
>presonal not understanding comes from this)
>  
>
Actually, your trouble comes from whoever described the Python's 
parameter-passing methodology in a way which is likely to confuse you.  
Better not to use incompatible terms than to stretch them to describe a 
different methodology.

Simply put, Python passes object references, and its names are just 
references to objects.  If you're a C/C++ person, Python objects are 
conceptually passed as PyObject pointers (with the same semantics in 
Jython, though not the same implementation).  When you pass a parameter 
into a function you're just binding a name (object reference) in the 
function to a given object.  When you re-bind that name within the 
function you're just pointing the name (object reference) at the other 
object, it has no effect on the original object (save reducing the 
number of references to it).

>Example 1.
>def ChangeList(ListToChange):
>	ListToChange[1] = "Line B"
>  
>
...

>So now function ChangeList.. is
>(1) def ChangeList(ListToChange):
>(2)	ListToChange[1] = "Line B"
>(3)	ListToChange[2] = "Line C"
>(4)	ListToChange = ["Line X","Line Y","Line Z"]
>  
>
Your last line is doing nothing useful.  It binds the name ListToChange 
(which is never referred to after this point) to a newly constructed 
list object.  This doesn't affect the original list passed into the 
function in any way.

>Result is the same (as it suppose to be..)
>["Line 1","Line 2","Line 3"]
>["Line 1","Line B","Line C"]
>
>
>But now... I am swaping line
>(1)def ChangeList(ListToChange):
>(2)	ListToChange[1] = "Line B"
>(3)	ListToChange = ["Line X","Line Y","Line Z"]
>(4)	ListToChange[2] = "Line C"
>  
>
Line 3 there is binding ListToChange to a new object, so line 4 is 
operating on that new list object, not the original object.  You don't 
return the new list object, so the last two lines are doing nothing.

>I have only swapped line 3 and 4.
>
>Result is unexpected
>["Line 1","Line 2","Line 3"]
>["Line 1","Line B","Line 3"]
>
>The last line in function is to change where ListToChange[2] reffers
>to.. but string is the same..
>Frankly I was expecting that ListToChange will change it's second
>(zero base) element... but it does not.
>Why?
>Excuse me if the question is stupid.
>  
>
Not stupid.  Just based on a poor description of how Python's argument 
passing works. Hope this one was somewhat more enlightening.

Have fun,
Mike

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/






More information about the Python-list mailing list