Some strange behaviours of default arguments

June Kim junaftnoon at nospamplzyahoo.com
Tue Oct 31 14:14:23 EST 2000


def add(a, list=[]):
    list.append(a)
    print list



(I think "list" is a local variable, and every time the function
is called without the optional argument, the variable is initiated
with a new object, blank list -- but Python 2.0 doesn't work that way)


>>> add(3)
[3]
>>> add(5)
[3,5]

Why is the local variable "list"  NOT initialized with the default
value every time I call it? Can anyone gimme some, if any,
reasonable explanation?

Thanks in advance,

June




More information about the Python-list mailing list