<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Atomized &#187; scope</title>
	<atom:link href="http://atomized.org/tag/scope/feed/" rel="self" type="application/rss+xml" />
	<link>http://atomized.org</link>
	<description>Fragmenting reality.</description>
	<lastBuildDate>Mon, 23 May 2011 23:46:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>When lexical scoping attacks</title>
		<link>http://atomized.org/2009/06/when-lexical-scoping-attacks/</link>
		<comments>http://atomized.org/2009/06/when-lexical-scoping-attacks/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 21:30:34 +0000</pubDate>
		<dc:creator>Ian</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scope]]></category>
		<category><![CDATA[wtf]]></category>

		<guid isPermaLink="false">http://atomized.org/?p=574</guid>
		<description><![CDATA[Consider the following Python code: bar = [] def test(): new = ['this', 'comes', 'from', 'test'] bar.extend(new) print bar print bar test() print bar This code generates this output: [] ['this', 'comes', 'from', 'test'] ['this', 'comes', 'from', 'test'] Which is to say, bar is empty before test(), has the contents of new inside of test(), [...]]]></description>
			<content:encoded><![CDATA[<p>Consider the following Python code:</p>
<pre language="python">
bar = []
def test():
    new = ['this', 'comes', 'from', 'test']
    bar.extend(new)
    print bar

print bar
test()
print bar
</pre>
<p>This code generates this output:</p>
<pre>
[]
['this', 'comes', 'from', 'test']
['this', 'comes', 'from', 'test']
</pre>
<p>Which is to say, <code>bar</code> is empty before <code>test()</code>, has the contents of <code>new</code> inside of <code>test()</code>, and continues to have that value when <code>test()</code> returns.</p>
<p>Now, look at this code:</p>
<pre language="python">
bar = []
def test():
    new = ['this', 'comes', 'from', 'test']
    bar = new
    print bar

print bar
test()
print bar
</pre>
<p>Which outputs:</p>
<pre>
[]
['this', 'comes', 'from', 'test']
[]
</pre>
<p>The value of <code>bar</code> is only changed inside <code>test()</code>. Upon further reflection, I realized that the assignment was assigning a new local variable inside the <code>test()</code> scope, whereas accessing <code>bar</code> returns the variable from the outer scope, which is then modified.</p>
<p>This behavior doesn’t make a whole lot of sense to me.</p>
]]></content:encoded>
			<wfw:commentRss>http://atomized.org/2009/06/when-lexical-scoping-attacks/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

