<div dir="ltr">Here's <a href="https://github.com/minrk/ipython_extensions/blob/master/closure.py">an example</a> of what two approaches might look like.<div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">

On Fri, May 17, 2013 at 5:23 AM, Jeffrey Tratner <span dir="ltr"><<a href="mailto:jtratner@gmail.com" target="_blank">jtratner@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<p>That's a good point about unbound locals. Thanks for the link to the extensions page.</p><div class="HOEnZb"><div class="h5">
<div class="gmail_quote">On May 17, 2013 8:15 AM, "Thomas Kluyver" <<a href="mailto:takowl@gmail.com" target="_blank">takowl@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div dir="ltr"><div><div>Hmm, that's an interesting idea. Running it under a def might cause some other problems, like UnboundLocalErrors when you try to modify variables. Another approach might be a cell magic that remembered what globals were defined at the start of the cell, and removed any new ones after running the cell.<br>




<br></div>Perhaps it would be best to collect some examples of cases where this would be useful, to see what's the best way to do it. You can of course write an extension for it:<br><a href="http://ipython.org/ipython-doc/stable/config/extensions/index.html" target="_blank">http://ipython.org/ipython-doc/stable/config/extensions/index.html</a><br>




<br></div>Best wishes,<br>Thomas<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 17 May 2013 04:55, Jeffrey Tratner <span dir="ltr"><<a href="mailto:jtratner@gmail.com" target="_blank">jtratner@gmail.com</a>></span> wrote:<br>




<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><p>Hi all,</p><p>I've been using ipython notebook for a project at work and I really enjoy it. The only issue that I have is that sometimes using the code in cells and editing code on the fly means that you unintentionally define globals without meaning to. Clearly you can define a new function, but that also pollutes the name space and just feels weird. </p>








<p>Javascript has a convention of wrapping code in an immediately executed function and I know that someone has proposed adding something like a given keyword to python to accomplish something similar, but it'd be nice to be able to do it without adding an extra level of indentation and to be able to do it in 2.7.</p>








<p>I'm imagining a syntax like %%closure that consumes a cell and executes it as if it were underneath a def statement. This has the benefit of reducing the level of indentation as well as doing things like this:</p>






<p>within_cell_variable = xyz</p><p># do something</p><p>del within_cell_variable</p><p>---</p><p>or if you need to iterate over something, suddenly you've polluted the namespace with a random off variable.</p>

<p>I'd love to have this, I think it would be useful in the context of a notebook and reduce the amount of def statements used only to encapsulate variables. It's also kinda magical I guess.</p><p>Best,<br>

<br>Jeff</p>
</div>
<br>_______________________________________________<br>
IPython-dev mailing list<br>
<a href="mailto:IPython-dev@scipy.org" target="_blank">IPython-dev@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-dev" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-dev</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<br>
IPython-dev mailing list<br>
<a href="mailto:IPython-dev@scipy.org" target="_blank">IPython-dev@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-dev" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-dev</a><br>
<br></blockquote></div>
</div></div><br>_______________________________________________<br>
IPython-dev mailing list<br>
<a href="mailto:IPython-dev@scipy.org">IPython-dev@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-dev" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-dev</a><br>
<br></blockquote></div><br></div>