Message60816
The tutorial for 2.4.1, section 4.6 Defining Functions
states that formal parameters are introduced into the
local symbol table, making all calls call by object
reference.
The footnote points out that this means that changes to
mutable objects will be seen by the caller. This is
also illustrated in the example involving calling the
list method append.
It would be helpful if the example could point out that
passing a value such as 1 passes an immutable object
(the constant integer value 1), and so it is impossible
to write code such as:
a = 1
def f(val):
val = val + 1
and expect that after the call a == 2, even though val
== 2.
My experience is that this is a confusing issue for new
users, who may not understand that val = val + 1 tosses
the object reference value passed, replacing it with a
new local object. New users tend to see val as a
mutable object, since we just changed the value, didn't
we? :) |
|
Date |
User |
Action |
Args |
2008-01-20 09:58:10 | admin | link | issue1296434 messages |
2008-01-20 09:58:10 | admin | create | |
|