Home  >  Article  >  Backend Development  >  为什么 Python 不是 lexical scoping?

为什么 Python 不是 lexical scoping?

WBOY
WBOYOriginal
2016-06-06 16:23:051175browse

这似乎背离了现代程序设计语言的一般设计思路。

比如

<span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
    <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="k">def</span> <span class="nf">inner</span><span class="p">():</span>
        <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="n">x</span> <span class="o">=</span> <span class="mi">3</span>
    <span class="k">print</span> <span class="n">inner</span><span class="p">()</span>

回复内容:

这段代码是lexical scoping,静态作用域是指我们可以根据代码文本的位置,确定变量的存在区域。

按照python的LEGB(Local,Enclosing,Global,Built-in)规则,当调用inner()时,x实际上是在foo的scope中找到的。inner之所能够访问foo中的x,是因为inner is inside the text of foo,这正是lexical的含义
为什么 Python 不是 lexical scoping?
Bash是Dynamic Scoping的
<code class="language-bash"><span class="nv">x</span><span class="o">=</span><span class="m">1</span>                                                                                                 
<span class="k">function</span> g <span class="o">()</span> <span class="o">{</span> <span class="nb">echo</span> <span class="nv">$x</span> <span class="p">;</span> <span class="nv">x</span><span class="o">=</span><span class="m">2</span> <span class="p">;</span> <span class="o">}</span> 
<span class="k">function</span> f <span class="o">()</span> <span class="o">{</span> <span class="nb">local </span><span class="nv">x</span><span class="o">=</span><span class="m">3</span> <span class="p">;</span> g <span class="p">;</span> <span class="o">}</span> 
f  <span class="c">#f中的g执行时打印出的x是3而不是1</span>
<span class="nb">echo</span> <span class="nv">$x</span>  <span class="c">#这时打印出的x是1</span>
</code>
你以为Python是

<code class="language-ocaml"><span class="k">let</span> <span class="n">foo</span> <span class="bp">()</span> <span class="o">=</span>
  <span class="k">let</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">in</span>
    <span class="k">let</span> <span class="n">inner</span> <span class="bp">()</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">in</span>
      <span class="k">let</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">3</span> <span class="k">in</span>
        <span class="n">print</span> <span class="o">(</span><span class="n">inner</span> <span class="bp">()</span><span class="o">)</span>
</code>
python的闭包里的自由变量是按引用传递的,而不是按值传递,所以会有这个结果。
这和scoping没有关系。
C++的lambda就可以选择capture by copy或者capture by reference. 根据之前阅读Python源码的经验(如果记错请指正),在题主的例子里面,这个inner是一个闭包。闭包在Python里面的实现方式是保存一个通往外部namespace的指针(可以理解成一个dictionary)。

楼主可以参看这个例子
<code class="language-python"><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
	<span class="k">def</span> <span class="nf">inner</span><span class="p">():</span>
		<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
	<span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
	<span class="k">print</span> <span class="n">inner</span><span class="p">()</span> <span class="c"># output 2</span>
	<span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
	<span class="k">print</span> <span class="n">inner</span><span class="p">()</span> <span class="c"># output 3</span>
</code>

这分明就是lexical scoping嘛,譬如说等价的C#代码


<code class="language-csharp"><span class="k">void</span> <span class="nf">Foo</span><span class="p">()</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">x</span><span class="p">=</span><span class="m">1</span><span class="p">;</span>
    <span class="n">Func</span><span class="p"><span class="kt">int</span><span class="p">></span> <span class="n">inner</span> <span class="p">=</span> <span class="p">()=></span><span class="n">x</span><span class="p">+</span><span class="m">1</span><span class="p">;</span>
    <span class="n">x</span><span class="p">=</span><span class="m">3</span><span class="p">;</span>
    <span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">inner</span><span class="p">());</span>
<span class="p">}</span>
</span></code>
把楼主的代码改写成 lua 可以看看 Python 和 Lua 在处理上的不同:

第一个例子结果都是一样的,因为变量绑定的是引用而不是值。第二个例子:

<code class="language-lua"><span class="k">function</span> <span class="nf">foo</span><span class="p">()</span>
	<span class="k">function</span> <span class="nf">inner</span><span class="p">()</span>
		<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
	<span class="k">end</span>
	<span class="kd">local</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">3</span>
	<span class="nb">print</span><span class="p">(</span><span class="n">inner</span><span class="p">())</span>
<span class="k">end</span>

<span class="n">foo</span><span class="p">()</span>
</code>
题主所说的 现代化的编程语言指的是什么? js经过这么多代的更新迭代,现在也是这样~

<code class="language-js"><span class="p">(</span><span class="kd">function</span> <span class="nx">foo</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">function</span> <span class="nx">inner</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">x</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span>
  <span class="p">}</span>
  <span class="nx">x</span> <span class="o">=</span> <span class="mi">3</span><span class="p">;</span>
  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">inner</span><span class="p">());</span>
<span class="p">})();</span>
</code>
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;inner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
<span class="n">foo</span><span class="p">()</span>  
<span class="c"># 输出4</span>
我觉得完全没有背离啊。。
输出感觉当前 x 走的 這個問題很好回答:x是inner函數的環境變量,所以在inner的定義中出現的x其實就是對定義外面的,也就是inner的環境變量的一個引用而已。

函數只有在被調用的時候才會執行,你前面將置為1,後面又改為3,不過是改變了x的引用值而已,相當於給x重新賦值。然後執行inner函數,使用x所對應的值為3,因此答案就是4了。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn