從程序看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'

最近搜python相關問題的時候,看到這個挺有意思的文章,在這分享給大家,希望大家學習python的時候有所領悟


發佈了4 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章