[Novice] Default argument value in function definition

Peter Otten __peter__ at web.de
Thu Jul 15 19:20:39 EDT 2004


Tito wrote:

> How is the value of L conserved between funtion calls?
> Can someone explain the mechanism to me?

A function is just a callable object. Its default arguments are stored as a
tuple in the func_defaults attribute. You can easily experiment with these
kind of things in the interpreter:

>>> def f(a, items=[]):
...     items.append(a)
...
>>> f(1)
>>> f(2)
>>> f(3)
>>> f.func_defaults
([1, 2, 3],)
>>> f(4)
>>> f.func_defaults
([1, 2, 3, 4],)
>>>

Peter




More information about the Python-list mailing list