<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div><br></div>Those of you tracking the Cuba working group will have seen that Brython is to be a topic [0].  <br><br>That's "Python is the browser", which I'm eager to try (haven't yet).<br><br></div><div>Brython is a segue to JavaScript, which I'm relearning from scratch ('The Good Parts' etc.).<br></div><div><br></div>Over on mathfuture I'm continuing to discuss what I call the Lambda Calc track [1] where we introduce programming as a math-relevant way of thinking / activity / skill.  A long-running theme here on edu-sig already.<br><br></div>In comparing JavaScript and Python, I'm learning quite a bit.<br><br></div>Example:<br><br></div>JavaScript allows closures wherein a function in a function retains its memory of the variables (names) in the surrounding scope.  The following example of a closure is from:<br><br></div>Speaking JavaScript<br>by Axel Rauschmayer<br>Publisher: O'Reilly Media, Inc.<br>Release Date: March 2014<br>ISBN: 9781449365035<br><br></div><div>in the intro section on Closures.<br></div><div><div><div class="">
    
      
        
          <br></div><br>function createIncrementor(start) <br>         {return function () {          <br>          start++;         <br>          return start;    }}<br><br></div>... which gives the following behavior:<br><br>> var inc = createIncrementor(5);<br>> inc()<br>6<br>> inc()<br>7<br>> inc()<br>8<br><br></div>Lets try that same thing in Python.<br><br>def createIncrementor(x):<br>        def increm():<br>                x += 1<br>                return x<br>        return increm<br><br>At first it seems not to work (not just seems, it doesn't):<br><br>>>> from closure import createIncrementor<br>>>> inc = createIncrementor(5)<br>>>> inc()<br>Traceback (most recent call last):<br>  File "<pyshell#30>", line 1, in <module><br>    inc()<br>  File "/Users/kurner/Documents/classroom_labs/closure.py", line 4, in increm<br>    x += 1<br>UnboundLocalError: local variable 'x' referenced before assignment<br><br></div>That's where the keyword nonlocal comes in.  <br><br>The increm function was originally looking in the global namespace for x whereas we need it to look "close to home" (at the originally surrounding scope):<br><br>def createIncrementor(x):<br>        def increm():<br>                nonlocal x<br>                x += 1<br>                return x<br>        return increm<br><br></div>>>> from closure import createIncrementor<br>>>> inc = createIncrementor(5)<br>>>> inc()<br>6<br>>>> inc()<br>7<br>>>> inc()<br>8<br><br></div>Voila, a closure.<br><br></div><div>Closures turn out to be really important in JavaScript thanks to its weak modularization model i.e. namespaces are difficult to segregate minus such patterns.  Closures get used to mitigate name collisions.  <br><br>Python is less dependent on that pattern, given it has other ways to confine names to local scopes.<br></div><div><br></div>Kirby<br><br>[0]  <a href="http://tinyurl.com/hpgrebf">http://tinyurl.com/hpgrebf</a>  (Brython sprint / Cuba)<br><br>[1]  <a href="http://tinyurl.com/jbpef72">http://tinyurl.com/jbpef72</a>  (mathfuture posting)<br></div>