从程序看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的时候有所领悟


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值