[Edu-sig] this is interesting
Kirby Urner
kurner at oreillyschool.com
Wed Jun 8 09:14:27 CEST 2011
On Tue, Jun 7, 2011 at 9:51 PM, michel paul <mpaul213 at 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 'clear' after being
> called. He meant to create an empty list and ended up with something like
> this.
>
> What's a good way to explain what's going on?
>
> - Michel
>
>
Yeah good example.
I talk about the "function mouth" as likewise a "castle gate" where "guards"
(the parameters) show up to take the values passed to them (as arguments).
Some guards aren't met with a value and so go maybe over to this closet (of
default objects) and haul out whatever they're supposed to work with by
default.
They might append to it (like your history above).
Any time a value (object) comes in as an argument, they use that instead,
but the thing in the closet is always there.
In a primitive way, functions get to have state between calls in this way, a
hint that generators are to come, when the fish will finally walk upon land
(generators evolve from functions in this story).
Kirby
>
>
> --
> ==================================
> "What I cannot create, I do not understand."
>
> - Richard Feynman
> ==================================
> "Computer science is the new mathematics."
>
> - Dr. Christos Papadimitriou
> ==================================
>
>
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20110608/2346dbeb/attachment.html>
More information about the Edu-sig
mailing list