On Tue, Jun 7, 2011 at 9:51 PM, michel paul <span dir="ltr"><<a href="mailto:mpaul213@gmail.com">mpaul213@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<font face="tahoma,sans-serif"><div>>> def f(n, history = []):</div><div><span style="white-space:pre-wrap">        </span> history.append(n)</div><div><span style="white-space:pre-wrap">        </span> return history</div>
<div><br></div><div>>>> f(1)</div><div>[1]</div><div>>>> f(2)</div><div>[1, 2]</div><div>>>> f(3)</div><div>[1, 2, 3]</div><div>>>> f(2)</div><div>[1, 2, 3, 2]</div><div>>>> f(1)</div>
<div>[1, 2, 3, 2, 1]</div><div>>>> f(1,[])</div><div>[1]</div><div><br></div><div>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.</div>
<div><br></div><div>What's a good way to explain what's going on?</div><div><br></div><div>- Michel</div><div><br></div></font></blockquote><div><br></div><div>Yeah good example.</div><div><br></div><div>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). </div>
<div><br></div><div>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. </div><div><br></div><div>They might append to it (like your history above). </div>
<div><br></div><div>Any time a value (object) comes in as an argument, they use that instead, but the thing in the closet is always there.</div><div><br></div><div>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).</div>
<div><br></div><div>Kirby</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><font face="tahoma,sans-serif"><div></div><div><br></div></font><br>
-- <br><div>==================================<br>"What I cannot create, I do not understand."</div>
<div><br></div><div>- Richard Feynman</div>==================================<br>"Computer science is the new mathematics."<div><br></div><div>- Dr. Christos Papadimitriou<br>==================================<br>
</div><br>
<br>_______________________________________________<br>
Edu-sig mailing list<br>
<a href="mailto:Edu-sig@python.org">Edu-sig@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/edu-sig" target="_blank">http://mail.python.org/mailman/listinfo/edu-sig</a><br>
<br></blockquote></div><br>