本文是从 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 i 1 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)
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