a wierd parameter passing behavior

Torvo Sollazzo mpradella at yahoo.it
Wed Jun 4 09:29:58 EDT 2003


Hi!

I found a strange parameter passing behavior in Python (v. 2.2.2).
Look at this piece of code:
def p(l) :
    l += [3]
    l[0] = 2

def p1(l) :
    l = l + [3]
    l[0] = 2

def f() :
    l = [1]
    p(l)
    print l
    l = [1]
    p1(l)
    print l

If I call f(), I obtain the following results:
[2,3]
[1]
Why p1() is different from p()? The list l should be passed by
reference in both cases, while it seems that "l = l + [3]" creates a
new local variable, also called l.

Any idea?
Many thanks,
TS




More information about the Python-list mailing list