http://www.pythonclub.org/hacks/evolution
不久前,在互联网上出现了一篇有趣的文章,讲的是对于同一个问题,不同层次的Python程序员编出的Python代码显示出了不同的风格,代码都很简单,有趣。
编程新手
def factorial(x):
return 1
return x * factorial(x - 1)
一年编程经验(学Pascal的)
def factorial(x):
i = 2
result result = result * i
return result
一年编程经验(学C的)
def fact(x): #{
while (i = x): #{
i += 1;
return result;
print(fact(6))
一年编程经验(读过 SICP)
@tailcall
if (x > 1): return (fact((x - 1), (acc * x)))
print(fact(6))
一年编程经验(Python)
def Factorial(x):
for i in xrange(2, x + 1):
return res
懒惰的Python程序员
def fact(x):
print fact(6)
更懒的Python程序员
f = lambda x: x and x * f(x - 1) or 1
Python 专家
fact = lambda x: reduce(int.__mul__, xrange( 2 , x + 1 ), 1 )
Python 黑客
import sys
def fact(x, acc= 1 ):
return acc
专家级程序员
from c_math import fact
大英帝国程序员
from c_maths import fact
Web 设计人员
def factorial(x):
#--- Code snippet from The Math Vault ---
#-------------------------------------------------
i = 1 #Thanks Adam
#result = result * i #It's faster to use *=
#result = int(result *= i) #??????
#result = int(str(result) * i)
return result
Unix 程序员
import os
os.system( 'factorial ' + str(x))
Windows 程序员
NULL = None
hOutputDevice,
lpWparam,
*dwReserved):
return NULL #Not implemented
while dwCounter
dwCounter += 1
hOutputDevice.write( '\n' )
import sys
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
企业级程序员
def new( cls , *args, **kwargs):
pass
class IntegralNumber(int, Number):
return new (int, self )
class InternalBase(object):
self .base = base.toInt()
def getBase( self ):
def __init__( self , ibase):
def getInstance( cls , ibase):
cls .__instance
cls .__instance = new ( cls , ibase)
def __init__( self , ibase):
raise NotImplementedError
result = new (IntegralNumber, 1 )
while i
i = i + new (IntegralNumber, 1 )
new (IntegralNumber, 2 ))).calculateFactorial(new (IntegralNumber, 6 ))