Python interpreter stores the default parameter list in a dict named func_defaults meeting a "def func(a=0, b=[], c={})" statement. So there is func.func_defaults = [0, [], {}].
When we specify an object in default parameter list of a function, python interpreter will keep its reference in the list. Therefore, an object as a default parameter of a function may change if you change it. For example:
def func(a = []):
a.append(0)
print a
When call the function in a python IDLE, we may have:
>>> func()
[0]
>>> func()
[0, 0]
>>> func()
[0, 0, 0]
>>> func([1])
[1, 0]
>>> func()
[0, 0, 0, 0]
You may find some detail example at http://effbot.org/zone/default-values.htm
When we specify an object in default parameter list of a function, python interpreter will keep its reference in the list. Therefore, an object as a default parameter of a function may change if you change it. For example:
def func(a = []):
a.append(0)
print a
When call the function in a python IDLE, we may have:
>>> func()
[0]
>>> func()
[0, 0]
>>> func()
[0, 0, 0]
>>> func([1])
[1, 0]
>>> func()
[0, 0, 0, 0]
You may find some detail example at http://effbot.org/zone/default-values.htm
Comments