On Tue, Jun 7, 2011 at 9:51 PM, michel paul mpaul...@gmail.com wrote:
def f(n, history = []):
history.append(n)
return history
f(1)
[1]
f(2)
[1, 2]
f(3)
[1, 2, 3]
f(2)
[1, 2, 3, 2]
f(1)
[1, 2, 3, 2, 1]
f(1,[])
[1]
A student wrote me wondering why his function wouldn't
In a message of Tue, 07 Jun 2011 21:51:31 PDT, michel paul writes:
def f(n, history = []):
history.append(n)
return history
snip
What's a good way to explain what's going on?
- Michel
Assuming that you have already taught about the difference between
mutable and immutable objects, ask
def f(n, history = []):
history.append(n)
return history
f(1)
[1]
f(2)
[1, 2]
f(3)
[1, 2, 3]
f(2)
[1, 2, 3, 2]
f(1)
[1, 2, 3, 2, 1]
f(1,[])
[1]
A student wrote me wondering why his function wouldn't 'clear' after being
called. He meant to create an empty list and ended up with
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1
On 06/08/2011 12:51 AM, michel paul wrote:
def f(n, history = []):
history.append(n)
return history
[...]
A student wrote me wondering why his function wouldn't 'clear' after being
called. He meant to create an empty list and ended up