一个Python程序员的进化

不久前,在互联网上出现了一篇有趣的文章,讲的是对于同一个问题,不同层次的Python程序员编出的Python代码显示出了不同的风格,代码都很简单,有趣。

编程新手
1 def factorial(x):
2     if x == 0:
3         return 1
4     else:
5         return x * factorial(x - 1) //递归!不简单啊!
6 print factorial(6)

一年编程经验(学Pascal的)
1 def factorial(x):
2     result = 1
3     i = 2
4     while i <= x:
5         resultresult = result * i
6         ii = i + 1
7     return result
8 print factorial(6)

一年编程经验(学C的)
1 def fact(x): #{
2     result = i = 1;   //连续赋值!
3     while (i <= x): #{
4         result *= i;
5         i += 1;
6     #}
7     return result;
8 #}
9 print(fact(6))

一年编程经验(读过 SICP)
1 @tailcall
2 def fact(x, acc=1):
3     if (x > 1): return (fact((x - 1), (acc * x)))  //递归!
4     else:       return acc
5 print(fact(6))

一年编程经验(Python)
1 def Factorial(x):
2     res = 1
3     for i in xrange(2, x + 1): //工具!
4         res *= i
5     return res
6 print Factorial(6)

懒惰的Python程序员
1 def fact(x):
2     return x > 1 and x * fact(x - 1) or 1 //语法!
3 print fact(6)

更懒的Python程序员
 
1 f = lambda x: x and x * f(x - 1) or 1 //匿名函数+语法!
2  print f(6)

Python 专家
1 fact = lambda x: reduce(int.__mul__, xrange(2, x + 1), 1) //匿名函数+工具+底层!
2 print fact(6)

Python 黑客
1 import sys
2 @tailcall
3 def fact(x, acc=1):
4     if x: return fact(x.__sub__(1), acc.__mul__(x)) //直接用底层!别人看不懂!
5     return acc
6 sys.stdout.write(str(fact(6)) + '\n')

专家级程序员
1 from c_math import fact //用标准库!
2 print fact(6)

大英帝国程序员
 
1 from c_maths import fact
2  print fact(6)

Web 设计人员
01 def factorial(x):
02     #-------------------------------------------------
03     #--- Code snippet from The Math Vault          ---
04     #--- Calculate factorial (C) Arthur Smith 1999 ---
05     #-------------------------------------------------
06     result = str(1)
07     i = 1 #Thanks Adam
08     while i <= x:
09         #result = result * i  #It's faster to use *=
10         #result = str(result * result + i)
11            #result = int(result *= i) #??????
12         result = str(int(result) * i)  //不断优化!
13         #result = int(str(result) * i)
14         i = i + 1
15     return result
16 print factorial(6)

Unix 程序员
01     import os
02     def fact(x):
03         os.system('factorial ' + str(x)) //代码量少!
04     fact(6)
05 Windows 程序员
06     NULL = None
07     def CalculateAndPrintFactorialEx(dwNumber,
08                                      hOutputDevice,
09                                      lpLparam,
10                                      lpWparam,
11                                      lpsscSecurity,
12                                      *dwReserved):
13         if lpsscSecurity != NULL:
14             return NULL #Not implemented
15         dwResult = dwCounter = 1
16         while dwCounter <= dwNumber:
17             dwResult *= dwCounter
18             dwCounter += 1
19         hOutputDevice.write(str(dwResult))
20         hOutputDevice.write('\n')
21         return 1
22     import sys
23     CalculateAndPrintFactorialEx(6, sys.stdout, NULL, NULL, NULL, //自个都晕!
24      NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)

企业级程序员

01 def new(cls, *args, **kwargs):
02     return cls(*args, **kwargs)
03 class Number(object):
04     pass
05 class IntegralNumber(int, Number):
06     def toInt(self):
07         return new (int, self)
08 class InternalBase(object):
09     def __init__(self, base):
10         self.base = base.toInt()
11     def getBase(self):
12         return new (IntegralNumber, self.base)
13 class MathematicsSystem(object):
14     def __init__(self, ibase):
15         Abstract
16     @classmethod
17     def getInstance(cls, ibase):
18         try:
19             cls.__instance
20         except AttributeError:
21             cls.__instance = new (cls, ibase)
22         return cls.__instance
23 class StandardMathematicsSystem(MathematicsSystem):
24     def __init__(self, ibase):
25         if ibase.getBase() != new (IntegralNumber, 2):
26             raise NotImplementedError
27         self.base = ibase.getBase()
28     def calculateFactorial(self, target):
29         result = new (IntegralNumber, 1)
30         i = new (IntegralNumber, 2)
31         while i <= target:
32             result = result * i
33             i = i + new (IntegralNumber, 1)
34         return result
35 print StandardMathematicsSystem.getInstance(new (InternalBase,
36 new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6))   //面向对象!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要成为一名Python程序员,首先你需要学习Python语言和编程基础知识。你可以在网上找到很多关于Python的教程,从基本的语法到更高级的知识,比如算法、数据结构、设计模式、网络编程等等。此外,你还应该学习游戏开发相关的知识,比如游戏策划、美术设计等。 ### 回答2: 要成为一名游戏的Python程序员,你需要掌握一些基本的技能和知识,并且有一定的实践经验。以下是几个步骤可以帮助你实现这个目标: 1. 学习Python编程语言:首先,你需要掌握Python编程语言的基础知识,包括变量、数据类型、条件语句、循环等。可以通过在线教程、自学资料或者参加培训课程来学习。 2. 熟悉Python的游戏开发库:Python有一些游戏开发库,如Pygame、Pyglet、Panda3D等,学习和使用这些库能够帮助你开发游戏。阅读相关文档、教程和示例代码,掌握库的基本操作和功能。 3. 实践项目:通过编写小型游戏项目来实践你的编程技能。可以从简单的游戏开始,比如一个迷宫或者打砖块游戏,逐渐增加难度和复杂度。通过实践项目,你可以不断提升自己的编程能力和理解游戏开发的流程。 4. 参与开源项目或团队合作:通过参与开源项目或与其他游戏程序员合作,你可以学习到更多高级概念和工程实践。这样的经验可以提高你的编程技能,同时也可以帮助你建立自己的专业网络。 5. 持续学习和改进:游戏开发是一个不断演进的领域,新技术和工具层出不穷。要成为一名优秀的游戏Python程序员,你需要不断学习和掌握新的技能和知识。阅读相关的书籍、博客、论坛,参加工作坊和培训课程是不断学习和改进的好方法。 总而言之,成为一名游戏的Python程序员需要时间和努力。通过学习Python编程语言、熟悉游戏开发库、实践项目、参与开源项目和不断学习改进,你可以逐步达到这个目标。 ### 回答3: 要成为一名游戏的Python程序员,你需要掌握一定的编程基础和相关的技术知识。下面是一些步骤可以帮助你实现这个目标: 1. 学习Python编程语言:首先,你需要学习Python的基础知识,包括语法、数据类型、函数等。可以通过参加在线课程、阅读相关书籍或观看教学视频来学习。 2. 学习游戏开发基础知识:了解游戏开发的基本概念,例如游戏循环、图形渲染、碰撞检测等。学习游戏设计模式能够帮助你更好地理解游戏的结构和组织。 3. 学习游戏开发框架和工具:掌握一些常用的Python游戏开发框架和工具,例如Pygame、Panda3D等。这些工具可以帮助你更快地构建游戏原型和实现游戏逻辑。 4. 实践项目:开发小型游戏项目是提高编程能力的重要方式。尝试设计和实现自己的游戏项目,通过解决实际问题来熟悉游戏开发流程和常用的编程技巧。 5. 参与开源项目:积极参与开源游戏项目可以提高你的合作能力和代码质量。你可以通过GitHub等平台找到感兴趣的项目,并为其贡献代码。 6. 不断学习和提升:游戏行业发展迅速,不断学习新的技术和工具是必要的。关注最新的游戏开发趋势和技术,参加相关的培训和会议,保持对新知识的求知欲。 总之,成为一名游戏的Python程序员需要一定的时间和努力,但只要坚持学习和实践,你将逐渐掌握必要的技能,并能够参与到有趣的游戏开发项目中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值