nikhil wrote:
Hi,
I am learning Python and came across this example in the Python Online
Tutorial
(http://docs.python.org/tutorial/controlflow.html#default-argument-values)
for Default Argument Values.
ex:
def func(a, L=[ ]):
L.append(a)
return L
print func(1)
print func(2)
print func(3)
_O/P_
[1]
[1,2]
[1,2,3]
Now my doubt is,
1) After the first function call, when ' L' (Reference to List
object) goes out of scope, why isn't it destroyed ?
2) It seems that this behavior is similar to static variable logic in
C language. Is it something similar ? OR
Is it related to scope rules in Python ?
------------------------------------------------------------------------
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
It is a well known Python gotcha. Take a read through the "Mutable
defaults for function/method arguments" section of
http://www.ferg.org/projects/python_gotchas.html#contents_item_6
Hope that helps your understanding.
--
Kind Regards,
Christian Witts
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor