一个Python程序员的进化

开发 后端
不久前,在互联网上出现了一篇有趣的文章,讲的是对于同一个问题,不同层次的Python程序员编出的Python代码显示出了不同的风格,代码都很简单,有趣。下面让我们一起来看看一个Python程序员是进阶的全过程。

不久前,在互联网上出现了一篇有趣的文章,讲的是对于同一个问题,不同层次的Python程序员编出的Python代码显示出了不同的风格,代码都很简单,有趣。

编程新手

def factorial(x):  
    if x == 0:  
        return 1  
    else:  
        return x * factorial(x - 1)  
print factorial(6) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

一年编程经验(学Pascal的)

def factorial(x):  
    result = 1 
    i = 2 
    while i <= x:  
        resultresult = result * i  
        ii = i + 1  
    return result  
print factorial(6) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

一年编程经验(学C的)

def fact(x): #{  
    result = i = 1;  
    while (i <= x): #{  
        result *= i;  
        i += 1;  
    #}  
    return result;  
#}  
print(fact(6)) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

一年编程经验(读过 SICP)

@tailcall  
def fact(x, acc=1):  
    if (x > 1): return (fact((x - 1), (acc * x)))  
    else:       return acc  
print(fact(6)) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

一年编程经验(Python)

def Factorial(x):  
    res = 1 
    for i in xrange(2, x + 1):  
        res *= i  
    return res  
print Factorial(6) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

懒惰的Python程序员

def fact(x):  
    return x > 1 and x * fact(x - 1) or 1  
print fact(6) 
  • 1.
  • 2.
  • 3.

更懒的Python程序员

f = lambda x: x and x * f(x - 1) or 1  
print f(6) 
  • 1.
  • 2.

Python 专家

fact = lambda x: reduce(int.__mul__, xrange(2, x + 1), 1)  
print fact(6
  • 1.
  • 2.

Python 黑客

import sys  
@tailcall 
def fact(x, acc=1):  
    if x: return fact(x.__sub__(1), acc.__mul__(x))  
    return acc  
sys.stdout.write(str(fact(6)) + '\n'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

专家级程序员

from c_math import fact  
print fact(6
  • 1.
  • 2.

大英帝国程序员

from c_maths import fact  
print fact(6
  • 1.
  • 2.

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 <= x:  
        #result = result * i  #It's faster to use *=  
        #result = str(result * result + i)  
           #result = int(result *= i) #??????  
        result = str(int(result) * i)  
        #result = int(str(result) * i)  
        i = i + 1 
    return result  
print factorial(6
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

Unix 程序员

import os  
def fact(x):  
    os.system('factorial ' + str(x))  
fact(6
  • 1.
  • 2.
  • 3.
  • 4.

Windows 程序员

NULL = None 
def CalculateAndPrintFactorialEx(dwNumber,  
                                 hOutputDevice,  
                                 lpLparam,  
                                 lpWparam,  
                                 lpsscSecurity,  
                                 *dwReserved):  
    if lpsscSecurity != NULL:  
        return NULL #Not implemented  
    dwResult = dwCounter = 1 
    while dwCounter <= dwNumber:  
        dwResult *= dwCounter  
        dwCounter += 1 
    hOutputDevice.write(str(dwResult))  
    hOutputDevice.write('\n')  
    return 1 
import sys  
CalculateAndPrintFactorialEx(6, sys.stdout, NULL, NULL, NULL,  
 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

企业级程序员

def new(cls, *args, **kwargs):  
    return cls(*args, **kwargs)  
   
class Number(object):  
    pass 
   
class IntegralNumber(int, Number):  
    def toInt(self):  
        return new (int, self)  
   
class InternalBase(object):  
    def __init__(self, base):  
        self.base = base.toInt()  
   
    def getBase(self):  
        return new (IntegralNumber, self.base)  
   
class MathematicsSystem(object):  
    def __init__(self, ibase):  
        Abstract  
   
    @classmethod 
    def getInstance(cls, ibase):  
        try:  
            cls.__instance  
        except AttributeError:  
            cls.__instance = new (cls, ibase)  
        return cls.__instance  
   
class StandardMathematicsSystem(MathematicsSystem):  
    def __init__(self, ibase):  
        if ibase.getBase() != new (IntegralNumber, 2):  
            raise NotImplementedError  
        self.base = ibase.getBase()  
   
    def calculateFactorial(self, target):  
        result = new (IntegralNumber, 1)  
        i = new (IntegralNumber, 2)  
        while i <= target:  
            result = result * i  
            i = i + new (IntegralNumber, 1)  
        return result  
   
print StandardMathematicsSystem.getInstance(new (InternalBase,  
new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6)) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.

【编辑推荐】

  1. 年度黑马Python 自省指南
  2. Ruby趋于人性化 比Python更易阅读?
  3. 2011年1月编程语言排行榜:Python称霸2010
  4. 学习python处理python编码
  5. 2011年2月编程语言排行榜:Python继续给力超PHP
责任编辑:陈贻新 来源: 外刊IT评论
相关推荐

2020-02-22 21:51:43

程序员Microsoft SServerSQL

2014-01-06 09:33:32

程序员管理

2020-10-05 21:13:37

程序员技能开发者

2016-12-21 11:35:55

Python程序员

2015-06-08 10:48:39

程序员程序员自白

2015-06-16 10:31:36

程序员

2020-07-10 09:55:15

程序员技能开发者

2021-07-01 07:43:41

项目程序员代码

2019-11-07 15:30:00

EmacsIDE

2015-05-13 14:06:03

程序员糟糕的程序员

2015-08-24 10:07:13

程序员bug

2010-10-18 11:39:41

程序员

2020-01-06 09:53:29

程序员

2019-04-22 10:25:52

程序员技术职场

2009-02-12 15:07:57

程序员创业经验

2012-04-12 14:49:31

程序员

2015-04-08 10:57:15

程序员程序员四年经历

2012-11-28 13:25:27

程序员

2023-12-26 18:47:32

2011-10-31 09:14:35

程序员
点赞
收藏

51CTO技术栈公众号