这似乎背离了现代程序设计语言的一般设计思路。
比如
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span class="n">foo</span><span class="p">()</span>
<span class="c"># 输出4</span>