[摘]Python 程序员的进化
转自:http://www.oschina.net/news/15319/evolution-of-a-python-programmer在综合资讯栏中看到这个帖子,觉得很有意思,转上来,最后面加上些自己的知识补遗~~~
==========================================================================================
本文是从 The Evolution of a Python Programmer 这篇文章翻译而来。
不久前,在互联网上出现了一篇有趣的文章,讲的是对于同一个问题,不同层次的程序员编出的Phthon代码显示出了不同的风格,代码都很简单,有趣。这篇文章的原始出处在这里,我把它整理了一下,并修改了几处错误。
编程新手
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
一年编程经验(学Pascal的)
def factorial(x):
result = 1
i = 2
while i1 and x * fact(x - 1) or 1
print fact(6)
更懒的Python程序员
f = lambda x: x and x * f(x - 1) or 1
print f(6)
Python 专家
fact = lambda x: reduce(int.__mul__, xrange(2, x + 1), 1)
print fact(6)
Python 黑客
import sys
@tailcall
def fact(x, acc=1):
if x: return fact(x.__sub__(1), acc.__mul__(x))
return acc
sys.stdout.write(str(fact(6)) + '\n')
专家级程序员
from c_math import fact
print fact(6)
大英帝国程序员
from c_maths import fact
print fact(6)
Web 设计人员
def factorial(x):
#-------------------------------------------------
#--- Code snippet from The Math Vault ---
#--- Calculate factorial (C) Arthur Smith 1999 ---
#-------------------------------------------------
result = str(1)
i = 1 #Thanks Adam
while i
页:
[1]