[Tutor] Python Function Doubt

nikhil nik.mis at gmail.com
Tue May 26 14:41:59 CEST 2009


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 ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090526/14558301/attachment-0001.htm>


More information about the Tutor mailing list