[Tutor] How to use function with default values ?

rishabh mittal mittalrishabh at gmail.com
Sat Aug 6 23:22:08 EDT 2016


Hi

I am new to python and come from C language background. I couldn't able to
understand this

 >>> def f(a, L=[]):

...     L.append(a)

...     return L

...

>>> print(f(1))

[1]

>>> print(f(2))

[1, 2]

>>> print(f(3))

[1, 2, 3]


>>> def f(a, L=10):

...     L=10+a

...     return L

...

>>> print(f(1))

11

>>> print(f(1))

11


In the first example if i call function f then it looks like L is treated
as a static object which is not reinitialized in the subsequent function
calls.

But in the second example, it seems to be reinitialized in the subsequent
 calls. Can anyone explain this or point me to the discussion if it is
asked before.



Thanks
Rishabh Mittal


More information about the Tutor mailing list