M
Mike
While trying to write a recursive function involving lists, I came
across some (to me) odd behavior which I don't quite understand. Here's
a trivial function showing the problem.
r.append(itm)
print r
I know the function is quite artificial, but it's for illustration only.
Why is "r" not being reset to the empty list on subsequent calls? It
seems like it should be reinitialized when not explicitly provided.
Thanks in advance.
Mike
across some (to me) odd behavior which I don't quite understand. Here's
a trivial function showing the problem.
for itm in l:>>> def f(l, r = []):
r.append(itm)
print r
[1, 2, 3, 1, 2, 3, 1, 2, 3]>>> a = [1,2,3]
>>> f(a) [1, 2, 3]
>>> f(a) [1, 2, 3, 1, 2, 3]
>>> f(a)
I know the function is quite artificial, but it's for illustration only.
Why is "r" not being reset to the empty list on subsequent calls? It
seems like it should be reinitialized when not explicitly provided.
Thanks in advance.
Mike