<?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>可可熊的窝 &#187; Lua</title>
	<atom:link href="http://cocobear.info/blog/tag/lua/feed/" rel="self" type="application/rss+xml" />
	<link>http://cocobear.info/blog</link>
	<description>K.I.S.S</description>
	<lastBuildDate>Tue, 17 Aug 2010 10:26:58 +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>读完了Programming in Lua (first edition)</title>
		<link>http://cocobear.info/blog/2009/04/08/read-programming-in-lua-first-edition/</link>
		<comments>http://cocobear.info/blog/2009/04/08/read-programming-in-lua-first-edition/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 08:19:11 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Lua]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/?p=613</guid>
		<description><![CDATA[花了差不多两周的时间读完了Lua Programming in Lua (first edition) ，没找到pdf格式的，一直在官网在线看的，不是很舒服。看的似乎有点慢，里面的英文很简单，讲的内容难点也不多，也没有太多的代码去动手实践，似乎是受别人打击了:-)（看到人家博客里写道：“花两天的时间过了一遍PIL”）。 Lua作为一个脚本语言在很多地方与Python有相似之处，不过小巧了很多，数据类型、表达式、语句等都很精简，但是这并不影响Lua成为一个流行的脚本语言，灵活的table成就了Lua的强大，table可以演化出list,dict，更强大的是table可以实现OO，Lua并没有在语言级提供OO的实现，不过Lua的作者在PIL中展示了使用table完全可以实现近乎完美的OO编程，我觉得这是对整天鼓吹OO思想的人一个极大讽刺，OO其实没什么，C语言中使用结构体，函数指针也可以有模有样的进行OO编程。 Lua中也有很多函数式编程语言的影子，所以很多人把Lua和Scheme归为了近亲，不过俺不了解Scheme，对函数式编程也是一知半解，只能凑个热闹。 Lua之所以在游戏中使用比较广泛，是因为它可以很好的和C/C++结合在一起，可以很方便的互操作，通过使用Stack的方式。还有一个很重要的原因，Lua提供的是搭积木的原料，而不是搭好的建筑，所以你可以很灵活的用自己的方式去写程序。比如你可以用table实现一个完整的OO系统，然后在OO上面写程序(这里有一点很重要，基于table实现OO是很轻量的，而不是语言级的OO)；你也可以用metatable来实现Python中很强大的filter,map,reduce。 Lua中的table有点类似道家学说里面的&#8221;道&#8221;,&#8221;道生一，一生二，二生三，三生万物&#8221;，而table就是Lua中的“道”。 还得提一点Lua是完全用ANSI C编写的。 cocobear@0-0 /home/cocobear/Codes/lua-5.1.4/src $ wc *.c *.h -l &#124; tail -1 17002 total]]></description>
			<content:encoded><![CDATA[<p>花了差不多两周的时间读完了<a href="http://www.lua.org/pil/index.html">Lua Programming in Lua (first edition) </a>，没找到pdf格式的，一直在官网在线看的，不是很舒服。看的似乎有点慢，里面的英文很简单，讲的内容难点也不多，也没有太多的代码去动手实践，似乎是受别人打击了:-)（看到人家博客里写道：“花两天的时间过了一遍PIL”）。</p>
<p><a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>作为一个脚本语言在很多地方与<a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">Python</a>有相似之处，不过小巧了很多，数据类型、表达式、语句等都很精简，但是这并不影响<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>成为一个流行的脚本语言，灵活的table成就了<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>的强大，table可以演化出list,dict，更强大的是table可以实现OO，<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>并没有在语言级提供OO的实现，不过<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>的作者在<a href="http://www.lua.org/pil/16.html">PIL中</a>展示了使用table完全可以实现近乎完美的OO编程，我觉得这是对整天鼓吹OO思想的人一个极大讽刺，OO其实没什么，C语言中使用结构体，函数指针也可以有模有样的进行OO编程。</p>
<p><a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>中也有很多函数式编程语言的影子，所以很多人把<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>和Scheme归为了近亲，不过俺不了解Scheme，对函数式编程也是一知半解，只能凑个热闹。</p>
<p><a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>之所以在游戏中使用比较广泛，是因为它可以很好的和C/C++结合在一起，可以很方便的互操作，通过使用Stack的方式。还有一个很重要的原因，<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>提供的是搭积木的原料，而不是搭好的建筑，所以你可以很灵活的用自己的方式去写程序。比如你可以用table实现一个完整的OO系统，然后在OO上面写程序(这里有一点很重要，基于table实现OO是很轻量的，而不是语言级的OO)；你也可以用metatable来实现<a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">Python</a>中很强大的filter,map,reduce。</p>
<p><a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>中的table有点类似道家学说里面的&#8221;道&#8221;,&#8221;道生一，一生二，二生三，三生万物&#8221;，而table就是<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>中的“道”。</p>
<p>还得提一点<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>是完全用ANSI C编写的。</p>
<blockquote><p>cocobear@0-0 /home/cocobear/Codes/<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">lua</a>-5.1.4/src $ wc *.c *.h -l | tail -1<br />
 17002 total</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://cocobear.info/blog/2009/04/08/read-programming-in-lua-first-edition/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python与Lua中的尾部调用优化</title>
		<link>http://cocobear.info/blog/2009/03/31/tail-call-optimization-in-python-lua/</link>
		<comments>http://cocobear.info/blog/2009/03/31/tail-call-optimization-in-python-lua/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 06:23:12 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Lua]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/?p=595</guid>
		<description><![CDATA[LUA: &#62; &#160; &#160; function foo &#40;n&#41; &#62;&#62; &#160; &#160; &#160; if n&#62; 0 then return foo&#40;n - 1&#41; end &#62;&#62; &#160; &#160; end &#62; foo&#40;100&#41; &#62; foo&#40;1000&#41; &#62; foo&#40;10000&#41; &#62; foo&#40;100000&#41; &#62; foo&#40;1000000&#41; 如果函数最后一句是return g(...)这样的形式，Lua将会把这句解释为goto g(x)，因为这里除了对g函数调用，再没有别的事做，也不需要保存堆栈里调用函数的信息。因此上面即使调用很多次也没有出现堆栈溢出的问题，把上面的代码转换为Python: PYTHON: &#62;&#62;&#62; def foo&#40;n&#41;: ... &#160; &#160; if n&#62;0: return foo&#40;n-1&#41; ... &#62;&#62;&#62; foo&#40;100&#41; &#62;&#62;&#62; foo&#40;1000&#41; ... &#160; [...]]]></description>
			<content:encoded><![CDATA[<div class="syntax_hilite"><span class="langName"><a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">LUA</a>:</span>
<div id="lua-5">
<div class="lua" style="font-family:monospace;">
<ol>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;</span> &nbsp; &nbsp; <span style="color: #4e4eff;">function</span> foo <span style="color: #993399;">&#40;</span>n<span style="color: #993399;">&#41;</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;&gt;</span> &nbsp; &nbsp; &nbsp; <span style="color: #4e4eff;">if</span> n<span style="color: #993399;">&gt;</span> <span style="color: #339933;color:#800000;">0</span> <span style="color: #4e4eff;">then</span> <span style="color: #4e4eff;">return</span> foo<span style="color: #993399;">&#40;</span>n - <span style="color: #339933;color:#800000;">1</span><span style="color: #993399;">&#41;</span> <span style="color: #4e4eff;">end</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;&gt;</span> &nbsp; &nbsp; <span style="color: #4e4eff;">end</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;</span> foo<span style="color: #993399;">&#40;</span><span style="color: #339933;color:#800000;">100</span><span style="color: #993399;">&#41;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;</span> foo<span style="color: #993399;">&#40;</span><span style="color: #339933;color:#800000;">1000</span><span style="color: #993399;">&#41;</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;</span> foo<span style="color: #993399;">&#40;</span><span style="color: #339933;color:#800000;">10000</span><span style="color: #993399;">&#41;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;</span> foo<span style="color: #993399;">&#40;</span><span style="color: #339933;color:#800000;">100000</span><span style="color: #993399;">&#41;</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;</span> foo<span style="color: #993399;">&#40;</span><span style="color: #339933;color:#800000;">1000000</span><span style="color: #993399;">&#41;</span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
如果函数最后一句是return g(...)这样的形式，<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>将会把这句解释为goto g(x)，因为这里除了对g函数调用，再没有别的事做，也不需要保存堆栈里调用函数的信息。因此上面即使调用很多次也没有出现堆栈溢出的问题，把上面的代码转换为<a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">Python</a>:</p>
<div class="syntax_hilite"><span class="langName"><a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">PYTHON</a>:</span>
<div id="python-6">
<div class="python" style="font-family:monospace;">
<ol>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;&gt;&gt;</span> <span style="color: #0088ff;font-weight:bold;">def</span> foo<span style="color: white;">&#40;</span>n<span style="color: white;">&#41;</span>:</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">... &nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">if</span> n<span style="color: #993399;">&gt;</span><span style="color: #00baff;color:#800000;">0</span>: <span style="color: #0088ff;font-weight:bold;">return</span> foo<span style="color: white;">&#40;</span>n-<span style="color: #00baff;color:#800000;">1</span><span style="color: white;">&#41;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">... </div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;&gt;&gt;</span> foo<span style="color: white;">&#40;</span><span style="color: #00baff;color:#800000;">100</span><span style="color: white;">&#41;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;&gt;&gt;</span> foo<span style="color: white;">&#40;</span><span style="color: #00baff;color:#800000;">1000</span><span style="color: white;">&#41;</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">...</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: white;">File</span> <span style="color: #b7c274;">&quot;&lt;stdin&gt;&quot;</span>, line <span style="color: #00baff;color:#800000;">2</span>, <span style="color: #0088ff;font-weight:bold;">in</span> foo</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; File <span style="color: #b7c274;">&quot;&lt;/stdin&gt;&lt;stdin&gt;&quot;</span>, line <span style="color: #00baff;color:#800000;">2</span>, <span style="color: #0088ff;font-weight:bold;">in</span> foo</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; File <span style="color: #b7c274;">&quot;&lt;/stdin&gt;&lt;stdin&gt;&quot;</span>, line <span style="color: #00baff;color:#800000;">2</span>, <span style="color: #0088ff;font-weight:bold;">in</span> foo</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7fff;">RuntimeError</span>: maximum recursion depth exceeded</div>
</li>
</ol>
</div>
</div>
</div>
<p>
很快出就现了堆栈溢出的问题，在这里表现为达到了递归调用的最大限制。<a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">Python</a>自身没有实现尾部调用优化，不过也可以通过实现一个decorator办法来实现:</p>
<div class="syntax_hilite"><span class="langName"><a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">PYTHON</a>:</span>
<div id="python-7">
<div class="python" style="font-family:monospace;">
<ol>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7f7f7f; font-style: italic;">#!/usr/bin/env python2.4</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7f7f7f; font-style: italic;"># This program shows off a <a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">python</a> decorator(</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7f7f7f; font-style: italic;"># which implements tail call optimization. It</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7f7f7f; font-style: italic;"># does this by throwing an exception if it is </span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7f7f7f; font-style: italic;"># it's own grandparent, and catching such </span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7f7f7f; font-style: italic;"># exceptions to recall the stack.</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0088ff;font-weight:bold;">import</span> <span style="color: #23ebc3;">sys</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0088ff;font-weight:bold;">class</span> TailRecurseException:</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #0088ff;font-weight:bold;">def</span> <span style="color: #ffff32;">__init__</span><span style="color: white;">&#40;</span><span style="color: #ff7fff;">self</span>, args, kwargs<span style="color: white;">&#41;</span>:</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #ff7fff;">self</span>.<span style="color: white;">args</span> = args</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #ff7fff;">self</span>.<span style="color: white;">kwargs</span> = kwargs</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0088ff;font-weight:bold;">def</span> tail_call_optimized<span style="color: white;">&#40;</span>g<span style="color: white;">&#41;</span>:</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #b7c274;">&quot;&quot;&quot;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b7c274;"> &nbsp;This function decorates a function with tail call</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b7c274;"> &nbsp;optimization. It does this by throwing an exception</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b7c274;"> &nbsp;if it is it's own grandparent, and catching such</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b7c274;"> &nbsp;exceptions to fake the tail call optimization.</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b7c274;"> &nbsp;</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b7c274;"> &nbsp;This function fails if the decorated</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b7c274;"> &nbsp;function recurses in a non-tail context.</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #b7c274;"> &nbsp;&quot;&quot;&quot;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #0088ff;font-weight:bold;">def</span> func<span style="color: white;">&#40;</span><span style="color: #993399;">*</span>args, <span style="color: #993399;">**</span>kwargs<span style="color: white;">&#41;</span>:</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; f = <span style="color: #23ebc3;">sys</span>._getframe<span style="color: white;">&#40;</span><span style="color: white;">&#41;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">if</span> f.<span style="color: white;">f_back</span> <span style="color: #0088ff;font-weight:bold;">and</span> f.<span style="color: white;">f_back</span>.<span style="color: white;">f_back</span> </div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">and</span> f.<span style="color: white;">f_back</span>.<span style="color: white;">f_back</span>.<span style="color: white;">f_code</span> == f.<span style="color: white;">f_code</span>:</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">raise</span> TailRecurseException<span style="color: white;">&#40;</span>args, kwargs<span style="color: white;">&#41;</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">else</span>:</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">while</span> <span style="color: #00baff;color:#800000;">1</span>:</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">try</span>:</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">return</span> g<span style="color: white;">&#40;</span><span style="color: #993399;">*</span>args, <span style="color: #993399;">**</span>kwargs<span style="color: white;">&#41;</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">except</span> TailRecurseException, e:</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; args = e.<span style="color: white;">args</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kwargs = e.<span style="color: white;">kwargs</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; func.__doc__ = g.__doc__</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #0088ff;font-weight:bold;">return</span> func</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">@tail_call_optimized</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0088ff;font-weight:bold;">def</span> factorial<span style="color: white;">&#40;</span>n, acc=<span style="color: #00baff;color:#800000;">1</span><span style="color: white;">&#41;</span>:</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #b7c274;">&quot;calculate a factorial&quot;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #0088ff;font-weight:bold;">if</span> n == <span style="color: #00baff;color:#800000;">0</span>:</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">return</span> acc</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #0088ff;font-weight:bold;">return</span> factorial<span style="color: white;">&#40;</span>n-<span style="color: #00baff;color:#800000;">1</span>, n<span style="color: #993399;">*</span>acc<span style="color: white;">&#41;</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0088ff;font-weight:bold;">print</span> factorial<span style="color: white;">&#40;</span><span style="color: #00baff;color:#800000;">10000</span><span style="color: white;">&#41;</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7f7f7f; font-style: italic;"># prints a big, big number,</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7f7f7f; font-style: italic;"># but doesn't hit the recursion limit.</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">@tail_call_optimized</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0088ff;font-weight:bold;">def</span> fib<span style="color: white;">&#40;</span>i, current = <span style="color: #00baff;color:#800000;">0</span>, next = <span style="color: #00baff;color:#800000;">1</span><span style="color: white;">&#41;</span>:</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #0088ff;font-weight:bold;">if</span> i == <span style="color: #00baff;color:#800000;">0</span>:</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">return</span> current</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #0088ff;font-weight:bold;">else</span>:</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">return</span> fib<span style="color: white;">&#40;</span>i - <span style="color: #00baff;color:#800000;">1</span>, next, current + next<span style="color: white;">&#41;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0088ff;font-weight:bold;">print</span> fib<span style="color: white;">&#40;</span><span style="color: #00baff;color:#800000;">10000</span><span style="color: white;">&#41;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7f7f7f; font-style: italic;"># also prints a big number,</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #7f7f7f; font-style: italic;"># but doesn't hit the recursion limit.</span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
代码来自<a href="http://code.activestate.com/recipes/474088/">http://code.activestate.com/recipes/474088/</a></p>
<p>在这段代码里tail_call_optimized是一个decorator，在执行factorial函数前，这个decorator先执行，tail_call_optimized中通过sys._getframe()这个方法会返回一个frame对象，包含了堆栈顶部的信息，当发现当前调用是一个递归调用:</p>
<div class="syntax_hilite"><span class="langName"><a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">PYTHON</a>:</span>
<div id="python-8">
<div class="python" style="font-family:monospace;">
<ol>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #0088ff;font-weight:bold;">if</span> f.<span style="color: white;">f_back</span> <span style="color: #0088ff;font-weight:bold;">and</span> f.<span style="color: white;">f_back</span>.<span style="color: white;">f_back</span> </div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">and</span> f.<span style="color: white;">f_back</span>.<span style="color: white;">f_back</span>.<span style="color: white;">f_code</span> == f.<span style="color: white;">f_code</span>:</div>
</li>
</ol>
</div>
</div>
</div>
<p>
则抛出一个异常，下面的代码则截获异常，继续执行，这样就避免了堆栈的使用，很巧妙的一种方式。</p>
<p>在<a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">python</a>-dev的邮件列表里，有人曾经做过一个<a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">Python</a>的尾调用优化的<a href="http://mail.python.org/pipermail/python-dev/2004-July/046150.html">补丁</a>，不过Guido拒绝了这个补丁:</p>
<blockquote><p>I'm not interested in adding this to the official <a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">Python</a> release.</p>
<p>One reason is that if an exception happens in such a tail-recursive<br />
call, the stack trace will be confusing.</p>
<p>Another reason is that I don't think it's a good idea to try to<br />
encourage a Scheme-ish "solve everything with recursion" programming<br />
style in <a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">Python</a>.</p>
<p>But feel free to maintain this as an independent modification, a la<br />
Stackless -- I'm sure there are people who would like to try this<br />
out.</p>
<p>--Guido van Rossum (home page: http://www.<a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">python</a>.org/~guido/)</p></blockquote>
<p></stdin></p>
]]></content:encoded>
			<wfw:commentRss>http://cocobear.info/blog/2009/03/31/tail-call-optimization-in-python-lua/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python与Lua分别实现一个计数器</title>
		<link>http://cocobear.info/blog/2009/03/31/write-a-counter-with-python-and-lua/</link>
		<comments>http://cocobear.info/blog/2009/03/31/write-a-counter-with-python-and-lua/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 04:47:15 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/?p=591</guid>
		<description><![CDATA[Lua: LUA: &#62; &#160; function newCounter &#40;&#41; &#62;&#62; &#160; &#160; local i = 0 &#62;&#62; &#160; &#160; return function &#40;&#41; &#160; -- anonymous function &#62;&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160;i = i + 1 &#62;&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160;return i &#62;&#62; &#160; &#160; &#160; &#160; &#160; &#160;end &#62;&#62; &#160; end [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>:</p>
<div class="syntax_hilite"><span class="langName"><a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">LUA</a>:</span>
<div id="lua-11">
<div class="lua" style="font-family:monospace;">
<ol>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;</span> &nbsp; <span style="color: #4e4eff;">function</span> newCounter <span style="color: #993399;">&#40;</span><span style="color: #993399;">&#41;</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;&gt;</span> &nbsp; &nbsp; <span style="color: #4e4eff;">local</span> i <span style="color: #993399;">=</span> <span style="color: #339933;color:#800000;">0</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;&gt;</span> &nbsp; &nbsp; <span style="color: #4e4eff;">return</span> <span style="color: #4e4eff;">function</span> <span style="color: #993399;">&#40;</span><span style="color: #993399;">&#41;</span> &nbsp; <span style="color: #7f7f7f; font-style: italic;">-- anonymous function</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;&gt;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i <span style="color: #993399;">=</span> i + <span style="color: #339933;color:#800000;">1</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;&gt;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #4e4eff;">return</span> i</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;&gt;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #4e4eff;">end</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;&gt;</span> &nbsp; <span style="color: #4e4eff;">end</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;</span> &nbsp; </div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;</span> &nbsp; c1 <span style="color: #993399;">=</span> newCounter<span style="color: #993399;">&#40;</span><span style="color: #993399;">&#41;</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;</span> <span style="color: #4e4eff;">print</span><span style="color: #993399;">&#40;</span>c1<span style="color: #993399;">&#40;</span><span style="color: #993399;">&#41;</span><span style="color: #993399;">&#41;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339933;color:#800000;">1</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;</span> <span style="color: #4e4eff;">print</span><span style="color: #993399;">&#40;</span>c1<span style="color: #993399;">&#40;</span><span style="color: #993399;">&#41;</span><span style="color: #993399;">&#41;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #339933;color:#800000;">2</span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>同<a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">Python</a>类似，也可以嵌套定义函数，不过<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>嵌套函数可以访问上层闭包函数的局部变量，而在这个内嵌函数中这些变量不是全局变量，也不是局部变量，而是一种upvalue，与C语言中的static修饰的变量类似，所以在这里可以利用这个特性完成这个计数器。</p>
<p><a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">Python</a>:</p>
<div class="syntax_hilite"><span class="langName"><a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">PYTHON</a>:</span>
<div id="python-12">
<div class="python" style="font-family:monospace;">
<ol>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #993399;">&gt;&gt;&gt;</span> <span style="color: #0088ff;font-weight:bold;">def</span> counter<span style="color: white;">&#40;</span>last=<span style="color: white;">&#91;</span><span style="color: #00baff;color:#800000;">1</span><span style="color: white;">&#93;</span><span style="color: white;">&#41;</span>:</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; ... &nbsp; &nbsp; <span style="color: white;">next</span> = last<span style="color: white;">&#91;</span><span style="color: #00baff;color:#800000;">0</span><span style="color: white;">&#93;</span> + <span style="color: #00baff;color:#800000;">1</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; ... &nbsp; &nbsp; <span style="color: white;">last</span><span style="color: white;">&#91;</span><span style="color: #00baff;color:#800000;">0</span><span style="color: white;">&#93;</span> = next</div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; ... &nbsp; &nbsp; <span style="color: #0088ff;font-weight:bold;">return</span> next</div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; ... </div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;<span style="color: #993399;">&gt;&gt;&gt;</span> counter<span style="color: white;">&#40;</span><span style="color: white;">&#41;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #00baff;color:#800000;">2</span></div>
</li>
<li style="font-weight: bold; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;<span style="color: #993399;">&gt;&gt;&gt;</span> counter<span style="color: white;">&#40;</span><span style="color: white;">&#41;</span></div>
</li>
<li style="font-weight: normal; vertical-align:top;color:#3A6A8B;">
<div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span style="color: #00baff;color:#800000;">3</span></div>
</li>
</ol>
</div>
</div>
</div>
<p>
今天看<a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>的时候觉得<a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">Python</a>也应该有比较简单的办法写个计数器，找到<a href="http://www.ibm.com/developerworks/library/l-pycon.html">一篇文章</a>介绍这个小技巧，其中有一句话：</p>
<blockquote><p>However, if named parameters are given mutable default values, the parameters can act as persistent memories of previous invocations. Lists, specifically, are handy mutable objects that can conveniently even hold multiple values. </p></blockquote>
<p><a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a>作为一个嵌入式（嵌入到其它语言中）的脚本语言，在很多地方与<a href="http://cocobear.info/blog/tag/python/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Python">Python</a>类似，这几天在看Programming in  <a href="http://cocobear.info/blog/tag/lua/" class="st_tag internal_tag" rel="tag" title="Posts tagged with Lua">Lua</a> 。</p>
]]></content:encoded>
			<wfw:commentRss>http://cocobear.info/blog/2009/03/31/write-a-counter-with-python-and-lua/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
