Differences between revisions 2 and 4 (spanning 2 versions)
Revision 2 as of 2006-04-25 05:55:14
Size: 605
Editor: HuangYi
Comment:
Revision 4 as of 2006-04-25 15:46:12
Size: 967
Editor: HuangYi
Comment:
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
=== 模拟range的参数传递方式 == [[TableOfContents]]

= 模拟range的参数传递方式 =
Line 30: Line 32:
= Action模式实现 =

 * ["Action Pattern Implement"]

 * 晚上看 [http://harkal.sylphis3d.com/2005/08/10/multithreaded-game-scripting-with-stackless-python/ 这篇好文]的时候, 看到里面有一部分讲到Action模式在游戏里面的应用, 感觉有点意思就随手写了写, 总算赶在熄灯之前搞定, 呵呵。

TableOfContents

模拟range的参数传递方式

刚才写程序的时候突然想到的, 模拟个range ( range([start],stop,[step]) ) 的参数机制。

   1 def myrange(stop,start=None,step=None):
   2   if not start:
   3     return range(stop)
   4   elif not step:
   5     return range(stop,start)
   6   else:
   7     return range(stop,start,step)

测试:

   1 >>> myrange(5)
   2 [0, 1, 2, 3, 4]
   3 >>> myrange(1,5)
   4 [1, 2, 3, 4]
   5 >>> myrange(1,5,2)
   6 [1, 3]

呵呵, python果然够灵活。

Action模式实现


huangyi/2006-04-25 (last edited 2009-12-25 07:11:12 by localhost)