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

Niki Iv niki_iv at abv.bg
Tue Feb 10 11:20:11 CET 2004


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)

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

MyList = ["Line 1","Line 2","Line 3"]
print MyList
ChangleList(MyList)
print MyList

Result is quite understandable (as in Pythin Bible is..)
["Line 1","Line 2","Line 3"]
["Line 1","Line B","Line 3"]

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

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"


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.



More information about the Python-list mailing list