<?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: Static (lexical) vs dynamic scoping</title>
	<atom:link href="http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/feed/" rel="self" type="application/rss+xml" />
	<link>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/</link>
	<description>Michel explores computing and assorted gadgetries</description>
	<lastBuildDate>Mon, 07 Dec 2009 23:12:37 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Weight</title>
		<link>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comment-4679</link>
		<dc:creator>Weight</dc:creator>
		<pubDate>Tue, 29 Apr 2008 17:47:59 +0000</pubDate>
		<guid isPermaLink="false">http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comment-4679</guid>
		<description>thx for the useful info</description>
		<content:encoded><![CDATA[<p>thx for the useful info</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ehird</title>
		<link>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comment-563</link>
		<dc:creator>ehird</dc:creator>
		<pubDate>Mon, 30 Apr 2007 14:10:50 +0000</pubDate>
		<guid isPermaLink="false">http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comment-563</guid>
		<description>Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; x = 3
&gt;&gt;&gt; def hello():
...     print x
... 
&gt;&gt;&gt; def world():
...     x = 5
...     hello()
... 
&gt;&gt;&gt; hello()
3
&gt;&gt;&gt; world()
3</description>
		<content:encoded><![CDATA[<p>Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)<br />
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin<br />
Type &#8220;help&#8221;, &#8220;copyright&#8221;, &#8220;credits&#8221; or &#8220;license&#8221; for more information.<br />
&gt;&gt;&gt; x = 3<br />
&gt;&gt;&gt; def hello():<br />
&#8230;     print x<br />
&#8230;<br />
&gt;&gt;&gt; def world():<br />
&#8230;     x = 5<br />
&#8230;     hello()<br />
&#8230;<br />
&gt;&gt;&gt; hello()<br />
3<br />
&gt;&gt;&gt; world()<br />
3</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hircus</title>
		<link>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comment-443</link>
		<dc:creator>hircus</dc:creator>
		<pubDate>Fri, 13 Apr 2007 23:02:36 +0000</pubDate>
		<guid isPermaLink="false">http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comment-443</guid>
		<description>The top-level namespace is normally an exception to the rule. Even in Scheme:

&lt;code&gt;(define (f) 42)
(define (g) (f))
(g) ;; 42
(define (f) 50)
(g) ;; 50&lt;/code&gt;

The neat thing about Python&#039;s lexical scope is that you can actually fake private variables: declare a local dictionary inside your constructor __init__, put all the private variables there (key = the name, value = initial value), and define all the accessor methods in the constructor as well.

Because of lexical scoping, they have access to this dictionary. Now attach them to the class: 

&lt;code&gt;class bean(object):
  def __init__(self):
    privates = {&quot;val&quot; : 0}
    def get():
      return privates[&quot;val&quot;]
    def set(x):
      privates[&quot;val&quot;] = x
      return
    self.get = get
    self.set = set
    return&lt;/code&gt;

dir(bean()) will show you that the privates are, well, privates. Note the neat trick that if you use self.method_name = internal_name, you don&#039;t need to take self as an argument (but you don&#039;t have access to the object&#039;s namespace, apart from privates). If you use classname.method_name = internal_name (i.e. bean.get = get), now get must take self as a parameter, but gets to use it as needed.

I lifted this from a fascinating discussion, that unfortunately I can&#039;t find at the moment. Will update when I manage to locate it.</description>
		<content:encoded><![CDATA[<p>The top-level namespace is normally an exception to the rule. Even in Scheme:</p>
<p><code>(define (f) 42)<br />
(define (g) (f))<br />
(g) ;; 42<br />
(define (f) 50)<br />
(g) ;; 50</code></p>
<p>The neat thing about Python&#8217;s lexical scope is that you can actually fake private variables: declare a local dictionary inside your constructor __init__, put all the private variables there (key = the name, value = initial value), and define all the accessor methods in the constructor as well.</p>
<p>Because of lexical scoping, they have access to this dictionary. Now attach them to the class: </p>
<p><code>class bean(object):<br />
  def __init__(self):<br />
    privates = {"val" : 0}<br />
    def get():<br />
      return privates["val"]<br />
    def set(x):<br />
      privates["val"] = x<br />
      return<br />
    self.get = get<br />
    self.set = set<br />
    return</code></p>
<p>dir(bean()) will show you that the privates are, well, privates. Note the neat trick that if you use self.method_name = internal_name, you don&#8217;t need to take self as an argument (but you don&#8217;t have access to the object&#8217;s namespace, apart from privates). If you use classname.method_name = internal_name (i.e. bean.get = get), now get must take self as a parameter, but gets to use it as needed.</p>
<p>I lifted this from a fascinating discussion, that unfortunately I can&#8217;t find at the moment. Will update when I manage to locate it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Karl</title>
		<link>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comment-399</link>
		<dc:creator>Karl</dc:creator>
		<pubDate>Fri, 06 Apr 2007 04:17:16 +0000</pubDate>
		<guid isPermaLink="false">http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comment-399</guid>
		<description>ok ok, i finally figured it out.  python is statically scoped.  here is a good example:

&gt;&gt;&gt; def a1():
...     def q(x):
...             print &#039;stat&#039;, x
...     def p(x):
...             return q(x)
...     def a2():
...             def q(x):
...                     print &#039;dyn&#039;, x
...             p(&#039;hello&#039;)
...     a2()
...  
&gt;&gt;&gt; a1()
stat hello

the example i was working from the book was in a language (&quot;Oz&quot;) was a dynamic language that still had variable declaration:

local P Q in
  proc {Q X} {Browse stat(X)} end
  proc {P X} {Q X} end
  local Q in
    proc {Q X} {Browse dyn(X)} end
    {P hello}
  end
end

so it is easier to create inner scopes.  with python, i needed a separate inner function for each layer of scope for a comparable example.</description>
		<content:encoded><![CDATA[<p>ok ok, i finally figured it out.  python is statically scoped.  here is a good example:</p>
<p>&gt;&gt;&gt; def a1():<br />
&#8230;     def q(x):<br />
&#8230;             print &#8217;stat&#8217;, x<br />
&#8230;     def p(x):<br />
&#8230;             return q(x)<br />
&#8230;     def a2():<br />
&#8230;             def q(x):<br />
&#8230;                     print &#8216;dyn&#8217;, x<br />
&#8230;             p(&#8216;hello&#8217;)<br />
&#8230;     a2()<br />
&#8230;<br />
&gt;&gt;&gt; a1()<br />
stat hello</p>
<p>the example i was working from the book was in a language (&#8220;Oz&#8221;) was a dynamic language that still had variable declaration:</p>
<p>local P Q in<br />
  proc {Q X} {Browse stat(X)} end<br />
  proc {P X} {Q X} end<br />
  local Q in<br />
    proc {Q X} {Browse dyn(X)} end<br />
    {P hello}<br />
  end<br />
end</p>
<p>so it is easier to create inner scopes.  with python, i needed a separate inner function for each layer of scope for a comparable example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Karl</title>
		<link>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comment-391</link>
		<dc:creator>Karl</dc:creator>
		<pubDate>Thu, 05 Apr 2007 05:15:15 +0000</pubDate>
		<guid isPermaLink="false">http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comment-391</guid>
		<description>update: after more experimenting, seems that python is dynamically scoped for function values:

**** naive example
&gt;&gt;&gt; def q(x):
...     print &#039;stat&#039;, x
... 
&gt;&gt;&gt; def p(x):
...     q_holder = q
...     return q_holder(x)
... 
&gt;&gt;&gt; p(&#039;dude&#039;)
stat dude
&gt;&gt;&gt; def q(x):
...     print &#039;dyn&#039;, x
... 
&gt;&gt;&gt; p(&#039;dude&#039;)
dyn dude
&gt;&gt;&gt; 

***** should show static scoping
&gt;&gt;&gt; def q(x):
...     print &#039;stat&#039;, x
... 
&gt;&gt;&gt; def p(x):
...     q_holder = q
...     return q_holder(x)
... 
&gt;&gt;&gt; def blah(x):
...     print &#039;dyn&#039;, x
... 
&gt;&gt;&gt; p(&#039;dude&#039;)
stat dude
&gt;&gt;&gt; q = blah
&gt;&gt;&gt; p(&#039;dude&#039;)
dyn dude

I guess you can&#039;t copy a function definition and save it from being rebound later....</description>
		<content:encoded><![CDATA[<p>update: after more experimenting, seems that python is dynamically scoped for function values:</p>
<p>**** naive example<br />
&gt;&gt;&gt; def q(x):<br />
&#8230;     print &#8217;stat&#8217;, x<br />
&#8230;<br />
&gt;&gt;&gt; def p(x):<br />
&#8230;     q_holder = q<br />
&#8230;     return q_holder(x)<br />
&#8230;<br />
&gt;&gt;&gt; p(&#8216;dude&#8217;)<br />
stat dude<br />
&gt;&gt;&gt; def q(x):<br />
&#8230;     print &#8216;dyn&#8217;, x<br />
&#8230;<br />
&gt;&gt;&gt; p(&#8216;dude&#8217;)<br />
dyn dude<br />
&gt;&gt;&gt; </p>
<p>***** should show static scoping<br />
&gt;&gt;&gt; def q(x):<br />
&#8230;     print &#8217;stat&#8217;, x<br />
&#8230;<br />
&gt;&gt;&gt; def p(x):<br />
&#8230;     q_holder = q<br />
&#8230;     return q_holder(x)<br />
&#8230;<br />
&gt;&gt;&gt; def blah(x):<br />
&#8230;     print &#8216;dyn&#8217;, x<br />
&#8230;<br />
&gt;&gt;&gt; p(&#8216;dude&#8217;)<br />
stat dude<br />
&gt;&gt;&gt; q = blah<br />
&gt;&gt;&gt; p(&#8216;dude&#8217;)<br />
dyn dude</p>
<p>I guess you can&#8217;t copy a function definition and save it from being rebound later&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Karl</title>
		<link>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comment-390</link>
		<dc:creator>Karl</dc:creator>
		<pubDate>Thu, 05 Apr 2007 05:05:46 +0000</pubDate>
		<guid isPermaLink="false">http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comment-390</guid>
		<description>Hi,

I&#039;m reading &quot;Concepts, Techniques and Models of Computer Programming&quot; and came across the section on static vs. dynamic scoping.  I whipped open my laptop to check whether python was statically scoped, and was fooled into thinking it was dynamically scoped using something very similar to your &quot;bad&quot; example.

A couple google searches later and I find this post, the perfect clarification.  Thanks!</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I&#8217;m reading &#8220;Concepts, Techniques and Models of Computer Programming&#8221; and came across the section on static vs. dynamic scoping.  I whipped open my laptop to check whether python was statically scoped, and was fooled into thinking it was dynamically scoped using something very similar to your &#8220;bad&#8221; example.</p>
<p>A couple google searches later and I find this post, the perfect clarification.  Thanks!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
