On 06/19/2013 12:17 PM, Ahmed Abdulshafy wrote:
I'm reading the Python.org tutorial right now, and I found this part rather
strange and incomprehensible to me>
Important warning: The default value is evaluated only once. This makes a
difference when the default is a mutable object such as a list, dictionary, or
instances of most classes
This code:
def f(a, L=[]):
L.append(a)
return L
does the same as this code:
M=[]
def f(a, L=M):
L.append(a)
return L
where it's slightly more obvious that the list is created once, and
modified with each call to the function (or rather with each call to the
function that does not supply its own value for L).
Gary Herron
print(f(1))
print(f(2))
print(f(3))
This will print
[1]
[1, 2]
[1, 2, 3]
How the list is retained between successive calls? And why?
--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
--
http://mail.python.org/mailman/listinfo/python-list