[Edu-sig] this is interesting

Laura Creighton lac at openend.se
Wed Jun 8 10:23:40 CEST 2011


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 the student to make a different
version, with the signature

def f2(n, history=())

and see if that gives the expected result.

Then get the student to _explain to you_ what's the difference between
tuples and lists that could result in this difference in behaviour.
This will move the discussion one of two ways.  Either to "why using 
default values for mutable objects in function signatures is almost
never what you want to do, and a discussion of what we can do about
this (use sentinels)" :-) or "lists are a bad idea, use tuples for 
everything" :-(.  If you are in conversation 2, you need to detour
through 'then why do we have a list datatype?  What is it good for?"

Laura


More information about the Edu-sig mailing list