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

From: Niki Iv (niki_iv_at_abv.bg)
Date: 02/10/04


Date: Tue, 10 Feb 2004 10:20:11 GMT

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.



Relevant Pages

  • Re: Explanation of Instance Variables in Python
    ... >>I am writing a chapter for teaching OOP in Python. ... attributes in general is critical to understanding its implementation of OOP. ... fish, not serving fish, though;-): ...
    (comp.lang.python)
  • Problem with Lexical Scope
    ... I am not completely knowledgable about the status of lexical scoping in ... Python, but it was my understanding that this was added in a long time ... def collect: ...
    (comp.lang.python)
  • Re: "Private" Member Variables
    ... I'm having trouble understanding to what extent I ... > should follow this policy in my Python code so I thought I'd ask the group. ... Holy cow, Scott! ... approach to information hiding isn't broken. ...
    (comp.lang.python)
  • Re: Nested function scope problem
    ... #> Python construct does. ... For me, Python variable behaves just like a C++ variable (a pointer, ... #> what the idfunction is to a Python programmer. ... At least in my understanding, which, up to now, was ...
    (comp.lang.python)
  • Re: why cannot assign to function call
    ... without offering evidence. ... The Python model binds a name to a particular Python object, ... understanding requires understanding first what ... The abstraction, and the requirement to ...
    (comp.lang.python)