从程序看python程序员的进化

编程新手

  1. def factorial(x):  
  2.     if x == 0:  
  3.         return 1  
  4.     else:  
  5.         return x * factorial(x - 1)  
  6. print factorial(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. 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'

分享到: 微信 更多