FAQ
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.
def f(l, r = []):
for itm in l:
r.append(itm)
print r

a = [1,2,3]
f(a)
[1, 2, 3]
f(a)
[1, 2, 3, 1, 2, 3]
f(a)
[1, 2, 3, 1, 2, 3, 1, 2, 3]

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

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 10 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedApr 15, '07 at 1:23a
activeApr 15, '07 at 12:23p
posts10
users7
websitepython.org

People

Translate

site design / logo © 2022 Grokbase