编程与软件工程gist.github.com
Python 程序员的演化
Evolution of a Python programmer
2010 — 2012,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这个 Gist 用同一个阶乘函数展示 Python 程序员的“进化”:从新手写法到 Web 2.0 调 API、“差不多就行”的近似公式、硬编码查表,再到 lambda/reduce 的专家一行流。评论区继续补充并修正这些版本,还比较了性能和数学库实现。
为什么经久不衰:它把编程风格、过度工程和偷懒智慧浓缩进一个小例子,所以多年后仍被反复转发和讨论。
HN 评论精选
用斯特林近似和硬编码查表来偷懒,讽刺只求“差不多”和只覆盖已知输入的实现。
# “差不多就行”程序员 import math def fact(x): math.sqrt(2*math.pi*n) * math.pow(n/math.e, n) print fact(6) # 更懒的程序员 def fact(x): factorials = {6:720} # 在计算器上找到的,以后有了再补 if factorials.has_key(x): return factorials[x] else: raise NotImplementedError print fact(6)
notaddicted查看英文原文
# "close enough" programmer import math def fact(x): math.sqrt(2*math.pi*n) * math.pow(n/math.e, n) print fact(6) # lazy-er programmer def fact(x): factorials = {6:720} # found on my calculator, add more as they come up if factorials.has_key(x): return factorials[x] else: raise NotImplementedError print fact(6)
指出所谓专家版有兼容性/语法问题,并给出 Python 3 和 Python 2 的修正版。
“Python 专家”版本跑不起来,但修起来不难: import operator as op import functools as f fact = lambda x: f.reduce(op.mul, range(1, x + 1)) print(fact(6)) 如果你用 Python 2.x,因为 reduce 在默认命名空间里,可以写得更短: import operator as op fact = lambda x: reduce(op.mul, xrange(1, x + 1)) print fact(6)
tomku查看英文原文
The "Python expert" version doesn't run, but it's not hard to fix: import operator as op import functools as f fact = lambda x: f.reduce(op.mul, range(1, x + 1)) print(fact(6)) If you're using Python 2.x, you can make it a bit shorter due to reduce being in the default namespace: import operator as op fact = lambda x: reduce(op.mul, xrange(1, x + 1)) print fact(6)
实测显示直接用 math.factorial 比两种手写/偷懒版本快得多。
如果有人感兴趣(我就是,因为我发现自己是个‘更懒的程序员’)—— 对三个函数做 timeit 计时: Lazy Programmer - 0.907521744301 Lazier Programmer - 1.0473810545812512 Using math.factorial - 0.12187403971609001
chewxy查看英文原文
If anyone's interested (I am because I found myself to be a 'Lazier programmer') - Timeit's time for three functions: Lazy Programmer - 0.907521744301 Lazier Programmer - 1.0473810545812512 Using math.factorial - 0.12187403971609001
用 gamma 函数实现阶乘,并指出它还能处理非整数输入。
如果你能接受浮点结果: from math import gamma def factorial(x): return gamma(x+1) 好处是它对非整数参数也能正确工作。
tzs查看英文原文
If you can live with floating point results: from math import gamma def factorial(x): return gamma(x+1) This has the advantage of working correctly for non-integer arguments.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选