Default Argument Question

Paulo J. Matos pocm at ecs.soton.ac.uk
Wed Oct 29 12:14:19 EDT 2008


Hi all,

Going through the tutorial brought up a question. Consider the functions:

def f(a, L=[]):
    L.append(a)
    return L

print f(3)
print f(9)
print f(7)

def f1(i = 0):
    i = i + 1
    print i

f1()
f1()
f1()
f1()

Since the f accumulates the values in L, I was expecting to see i
printing 1,2,3,4 but this doesn't happen.
Can someone please explain why and what is going on beneath the veil?

Cheers,

-- 
Paulo Jorge Matos - pocmatos @ gmail.com



More information about the Python-list mailing list