工具与资源github.com
Icecream:Python 调试别再只用 print()
Icecream: Never use print() to debug again in Python
2019 — 2021,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
Icecream 是一个 Python 调试库,让你用 ic() 代替 print():它会自动打印变量名、表达式和值,还支持检查函数执行、带颜色输出等,省去手写调试语句的麻烦。项目在 GitHub 上开源,2019 和 2021 两次登上 Hacker News 首页。
为什么经久不衰:print 调试是每个程序员的日常,这个库把最常用的调试动作做到极致顺手,多年后仍是 Python 生态里被反复推荐的经典小工具。
HN 评论精选
Python 3.8 的 f-string = 语法已内置类似功能,无需第三方库
用 Python 3.8+ 的 f-string 也能做到同样的事,只需在变量名后加一个等号:print(f"{d['key'][1]=}") 会输出 d['key'][1]='one'。
kissgyorgy查看英文原文
You can do the same thing with Python 3.8+ by using f-strings and just appending "=" to the variable name: >>> print(f"{d['key'][1]=}") d['key'][1]='one'
推荐同类工具 PySnooper,用装饰器追踪函数执行全过程
当代码行为和我的心智模型不符时,我会用 PySnooper。它和 Icecream 一样宣称“再也不用 print 调试”,只需给函数加个装饰器,运行时就会自动打印每行代码执行情况和变量的变化过程。
jshprentz查看英文原文
I use PySnooper[1] when code behavior deviates inscrutably from my mental model. Like Icecream, PySnooper exhorts, "Never use print for debugging again." A simple example: import pysnooper @pysnooper.snoop() def add_up(numbers): total = 0 for number in numbers: total += number return total add_up([123, 456]) When run, PySnooper prints the activity of the decorated function or method: $ python example.py Source path:... /home/joel/example.py Starting var:.. numbers = [123, 456] 08:46:58.742543 call 4 def add_up(numbers): 08:46:58.742692 line 5 total = 0 New var:....... total = 0 08:46:58.742722 line 6 for number in numbers: New var:....... number = 123 08:46:58.742755 line 7 total += number Modified var:.. total = 123 08:46:58.742787 line 6 for number in numbers: Modified var:.. number = 456 08:46:58.742818 line 7 total += number Modified var:.. total = 579 08:46:58.742847 line 6 for number in numbers: 08:46:58.742876 line 8 return total 08:46:58.742898 return 8 return total Return value:.. 579 Elapsed time: 00:00:00.000405 [1] https://github.com/cool-RR/PySnooper
推荐另一个极简调试工具 q,用法几乎无限制
还有一个更好记的替代品 q:只需 import q,它既能当函数用(q(x)),也能当变量用(q|x、q/x,利用不同运算符优先级),还能当装饰器用(@q),几乎任何场景都能快速插入调试输出。
nneonneo查看英文原文
For an even easier-to-remember alternative, there’s q: https://github.com/zestyping/q All you need is `import q`. q works like a function (q(x)), like a variable (q|x and q/x, so you get different operator precedences) and like a decorator (@q), so it can be used in practically any circumstance for a quick debug print. Plus, the name sounds like you’re interrogating something.
内置 breakpoint() 加调试器已足够好,print 调试的优势场景越来越少
我以前也大量用 print 调试,后来试了 Python 3.7 起内置的 breakpoint(),现在离不开 PDB 这类真正的调试器了。print() 只比 breakpoint() 少打几个字符而已,pytest 还有 --pdb 选项可在测试失败时直接进入调试会话。我想不出还有什么场景 print 调试比真正的调试器更好。
bow_查看英文原文
I used to do print debugging a lot. And then I tried using the `breakpoint()` call available since Python 3.7[1], and now I can't imagine doing any serious debugging without PDB (or any other debugger). (I mentioned Python, since this is linking to the Python-flavor of Icecream). Sure, there are still times when just calling print() is sufficient. But they are becoming less frequent. `print()` is only shorter than `breakpoint()` by just 5 - len(variable_name_to_print) characters after all. I know `import pdb; pdb.set_trace()` was available before, though `breakpoint()` is just a lot simpler, among others for the reasons they mentioned in the PEP. There is also the `--pdb` flag in pytest[2], that lets me jump into a PDB session upon test failure. All of this makes me think, what are the cases where print-level debugging is the better option in Python, compared to using an actual debugger? [1] https://www.python.org/dev/peps/pep-0553/ [2] https://docs.pytest.org/en/latest/usage.html#using-the-builtin-breakpoint-function
历次讨论
同类还值得读
直接在浏览器中启动 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