工具与资源gto76.github.io
Python 综合速查表
Comprehensive Python Cheatsheet (2018)
2019 — 2021,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这是一个单页的 Python 速查表,按主题分区罗列语法、内置类型与容器操作、常用标准库用法,并配上简短可运行的代码片段,供日常查阅和复习。作者是 gto76,2018 年发布后多次被 Hacker News 首页推荐,常被当作入门与随手备查的资料。
为什么经久不衰:内容密集、示例准确,一页就能从基础语法查到常用惯用法,所以多年后仍值得收藏翻阅。
HN 评论精选
分享自己在脚本入口加 traceback 与 pdb.post_mortem、异常时直接进调试器的开发模板。
我喜欢作者给出的基础脚本模板。不过我开始写新代码时,总是用一个带 traceback 和 pdb 的脚本开头:在 __main__ 里 try 调用 main(),捕获异常后先 traceback.print_exc() 再 pdb.post_mortem()。这样一旦有未捕获异常,我马上就能看到完整回溯,并直接进入调试器检查原因;我还会在代码里大量使用 assert,形成很好的“编辑-运行-调试”循环。
gorb314查看英文原文
I like the basic script template that the author provides. However, when I start development of new code I always use a script that starts off with traceback and pdb, something like this: #!/usr/bin/env python import traceback import pdb import sys def main(): # some WIP code that maybe raises an exception raise BaseException("oh no, exception!") return 0 if __name__ == "__main__": try: ret = main() except: traceback.print_exc() pdb.post_mortem() sys.exit(ret) This means that whenever an uncaught exception gets raised, I immediately get told what happened (full backtrace) plus I get dumped into the debugger, from where I can inspect why this happened. I liberally sprinkle assert()s through my code, so this gives me a good edit-run-debug cycle to work with.
质疑去重示例,认为用 set 更直接。
表里有几处至少值得商榷。去重为什么要写 no_duplicates = list(dict.fromkeys(<list>)),而不是直接用 list(set(<list>))?
omegote查看英文原文
There are several items that are dubious at best. Why would you use no_duplicates = list(dict.fromkeys(<list>)) instead of no_duplicates = list(set(<list>)) ?
可变默认参数会在调用间共享同一个列表,这一经典陷阱应在速查表中明确提及。
奇怪这里没有提到可变默认值。我被这个坑过不止一次:def f(xs=[]): xs.append(5); return xs,连续两次 print(f()) 会输出 [5] 和 [5,5]。尽管有这种糟糕的设计,我还是很喜欢 Python,但入门资料应该更常提醒这一点。
adenozine查看英文原文
I'm surprised to see mutable default values not mentioned. It's bitten me more than once to discover that: def f(xs = []): xs.append(5) return xs print(f()) print(f()) will print: [5] [5,5] and I love python despite this horrendous decision, but it should be mentioned more often in beginner resources. edit: thanks for the downvote? I've answered on the order of dozens of questions about this very mechanic on SO, via IRC, and more... it's a well-known confusing factor
认为 setdefault 命名误导,它实际是“键不存在才插入”,并非设置字典默认值。
是不是只有我觉得 dict.setdefault 这个名字很差?我在这里第一次见到它,本以为它会设置字典的默认值,结果它做的其实是 (self, key, default) => self[key] = (key in self ? self[key] : default)。我更希望它叫 dict.createifnotexists 之类的名字。
basil-rash查看英文原文
Is it just me or is dict#setdefualt a terribly named function? I just learned of it here, and I was excited that it would set the default value for the dictionary, when instead it performs something like: (self, key, default) => self[key] = (key in self ? self[key] : default) I'd prefer something like "dict#createifnotexists", but better.
历次讨论
同类还值得读
直接在浏览器中启动 Linux 内核
Boot a linux kernel right inside your browser.
Fabrice Bellard 的 JSLinux 页面,用 JavaScript 在浏览器里启动一个 Linux 内核,并提供一个可交互的终端环境。
工具与资源上首页 5 次 · 2011–2025bellard.orgHacker News 未公开功能与行为清单(2018)
A List of Hacker News's Undocumented Features and Behaviors (2018)
数据科学家 Max Woolf 整理的 Hacker News 未公开功能与行为清单,涵盖投票机制、排名算法、账号惩罚等社区运作细节。
工具与资源上首页 5 次 · 2019–2025github.comMapSCII:在终端中用盲文和 ASCII 渲染地图
MapSCII – Braille and ASCII map renderer for the console
MapSCII 是一个在命令行终端里渲染地图的开源工具,用盲文字符和 ASCII 字符把地理数据画成可交互浏览的字符地图。
工具与资源上首页 5 次 · 2017–2025github.com顶尖大学的免费在线课程
Free Online Courses from Top Universities
Open Culture 整理的一份免费在线课程合集,汇集了全球顶尖大学(如 MIT、斯坦福、耶鲁等)向公众开放的网络课程资源,涵盖音频、视频等多种形式。
工具与资源上首页 5 次 · 2010–2022openculture.com淘气字符串:容易引发用户输入问题的字符串清单
Naughty Strings: A list of strings likely to cause issues as user-input data
一个 GitHub 仓库,收录了大量容易让程序出问题的用户输入字符串,如特殊字符、emoji、SQL 注入片段、系统命令等,供开发者用于测试输入处理逻辑。
工具与资源上首页 4 次 · 2015–2020github.com