<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Rebinding variable outside of the local scope</title>
	<atom:link href="http://ruslanspivak.com/2009/10/07/rebinding-variable-outside-of-the-local-scope/feed/" rel="self" type="application/rss+xml" />
	<link>http://ruslanspivak.com/2009/10/07/rebinding-variable-outside-of-the-local-scope/</link>
	<description>Musings on dynamic languages and more than that</description>
	<lastBuildDate>Fri, 30 Apr 2010 17:09:55 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Ruslan Spivak</title>
		<link>http://ruslanspivak.com/2009/10/07/rebinding-variable-outside-of-the-local-scope/#comment-457</link>
		<dc:creator>Ruslan Spivak</dc:creator>
		<pubDate>Tue, 13 Oct 2009 06:55:12 +0000</pubDate>
		<guid isPermaLink="false">http://ruslanspivak.com/?p=130#comment-457</guid>
		<description>Hi xtian,

Probably my example didn&#039;t emphasize it, but &#039;outer&#039; function could be nested into another function (thus no global state modification) and all I was caring about was a variable in the outside scope and how to modify it.

Maybe the following example explains my intentions better than the old one:
&lt;pre style=&quot;color:#F8F8F8;background-color:#0C1021;font-size:8pt;&quot;&gt;
&lt;span style=&quot;color:#AEAEAE;font-style:italic;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color:#AEAEAE;font-style:italic;&quot;&gt;main function
&lt;/span&gt;&lt;span style=&quot;color:#FBDE2D;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color:#FF6400;&quot;&gt;outer&lt;/span&gt;():
    outer.error = &lt;span style=&quot;color:#FBDE2D;&quot;&gt;False&lt;/span&gt;

    &lt;span style=&quot;color:#AEAEAE;font-style:italic;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color:#AEAEAE;font-style:italic;&quot;&gt;inner function does all the job and modifies the flag
&lt;/span&gt;    &lt;span style=&quot;color:#FBDE2D;&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color:#FF6400;&quot;&gt;inner&lt;/span&gt;(index):
        &lt;span style=&quot;color:#FBDE2D;&quot;&gt;if&lt;/span&gt; index == 5:
            outer.error = &lt;span style=&quot;color:#FBDE2D;&quot;&gt;True&lt;/span&gt;

    &lt;span style=&quot;color:#FBDE2D;&quot;&gt;for&lt;/span&gt; index &lt;span style=&quot;color:#FBDE2D;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color:#F8F8F8;&quot;&gt;range&lt;/span&gt;(10):
        inner(index)

    &lt;span style=&quot;color:#FBDE2D;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color:#61CE3C;&quot;&gt;&#039;FAILURE&#039;&lt;/span&gt; &lt;span style=&quot;color:#FBDE2D;&quot;&gt;if&lt;/span&gt; outer.error &lt;span style=&quot;color:#FBDE2D;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color:#61CE3C;&quot;&gt;&#039;SUCCESS&#039;&lt;/span&gt;
&lt;/pre&gt;

&gt;&gt;&gt; outer()

Quite often I would use classes and avoid workarounds altogether by storing state in a class instance.</description>
		<content:encoded><![CDATA[<p>Hi xtian,</p>
<p>Probably my example didn&#8217;t emphasize it, but &#8216;outer&#8217; function could be nested into another function (thus no global state modification) and all I was caring about was a variable in the outside scope and how to modify it.</p>
<p>Maybe the following example explains my intentions better than the old one:</p>
<pre style="color:#F8F8F8;background-color:#0C1021;font-size:8pt;">
<span style="color:#AEAEAE;font-style:italic;"># </span><span style="color:#AEAEAE;font-style:italic;">main function
</span><span style="color:#FBDE2D;">def</span> <span style="color:#FF6400;">outer</span>():
    outer.error = <span style="color:#FBDE2D;">False</span>

    <span style="color:#AEAEAE;font-style:italic;"># </span><span style="color:#AEAEAE;font-style:italic;">inner function does all the job and modifies the flag
</span>    <span style="color:#FBDE2D;">def</span> <span style="color:#FF6400;">inner</span>(index):
        <span style="color:#FBDE2D;">if</span> index == 5:
            outer.error = <span style="color:#FBDE2D;">True</span>

    <span style="color:#FBDE2D;">for</span> index <span style="color:#FBDE2D;">in</span> <span style="color:#F8F8F8;">range</span>(10):
        inner(index)

    <span style="color:#FBDE2D;">return</span> <span style="color:#61CE3C;">'FAILURE'</span> <span style="color:#FBDE2D;">if</span> outer.error <span style="color:#FBDE2D;">else</span> <span style="color:#61CE3C;">'SUCCESS'</span>
</pre>
<p>&gt;&gt;&gt; outer()</p>
<p>Quite often I would use classes and avoid workarounds altogether by storing state in a class instance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: xtian</title>
		<link>http://ruslanspivak.com/2009/10/07/rebinding-variable-outside-of-the-local-scope/#comment-451</link>
		<dc:creator>xtian</dc:creator>
		<pubDate>Thu, 08 Oct 2009 12:38:07 +0000</pubDate>
		<guid isPermaLink="false">http://ruslanspivak.com/?p=130#comment-451</guid>
		<description>Nice post! Your second workaround is a bit dodgy, though - rather than modifying non-local state you&#039;re really modifying global state. If you were returning the inner function, rather than calling it, the closed-over state would be shared between different calls to outer.

I often use a 1-element list as the state box for a mutating closure.

*sigh* I love closures.</description>
		<content:encoded><![CDATA[<p>Nice post! Your second workaround is a bit dodgy, though &#8211; rather than modifying non-local state you&#8217;re really modifying global state. If you were returning the inner function, rather than calling it, the closed-over state would be shared between different calls to outer.</p>
<p>I often use a 1-element list as the state box for a mutating closure.</p>
<p>*sigh* I love closures.</p>
]]></content:encoded>
	</item>
</channel>
</rss>