编程与软件工程github.com编辑精选
WTFPython:通过意外片段理解 Python
WTFPython: Exploring and understanding Python through surprising snippets
2021 — 2022,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这是 GitHub 上的一个知名仓库,收集了大量看起来会报错、实际却能运行(或反之)的 Python 小片段,并逐条解释背后的机制,比如可变默认参数、用 `is` 比较字符串、`+=` 对类属性与实例属性的不同处理等。它更像一本“反直觉行为图鉴”,帮人理解 Python 的对象模型、作用域和求值规则,而不是教你写项目。
为什么经久不衰:每个例子都短小可运行、能亲手验证,把语言细节变成肌肉记忆,多年后重读仍能帮你认出真实代码里的隐蔽 bug。
HN 评论精选
补充一个例子:元组走 `+` 会新建实例属性,列表走 `+=` 原地修改类属性。
我有个略让人意外的片段,我相信他们没有收录。它讲的是类属性和实例属性在 "X += Y" 下的交互行为。Python 会把它展开成 "X = X + Y",但只在 X 实现了 "+" 而没有实现 "+=" 时才这样: class A: a = (1,) # 元组不可变,没有 "+=" b = [1,] # 列表有 "+=" obj = A() obj.a += (2,) # 这会创建 obj.a obj.b += (2,) # 这会修改 A.b print(A.a, A.b) 输出是 "(1,) [1,2]"。
praptak查看英文原文
I have a mildly surprising snippet which I believe they don't have. It's about interaction of class and instance attributes with behavior of "X += Y". Python expands it to "X = X + Y" but only if X implements "+" but not "+=" class A: a = (1,) # tuples are immutable and don't have "+=" b = [1,] # lists have "+=" obj = A() obj.a += (2,) # this creates obj.a obj.b += (2,) # this modifies A.b print(A.a, A.b) The snippet prints "(1,) [1,2]".
作者提供了基于 pyodide 的网页版,可在浏览器里直接跑这些例子。
你好,我是这个仓库的维护者,感谢分享 :) 如果有人愿意一边开着解释器一边过这些例子,可以看看 https://www.wtfpython.xyz/ 。它是用 pyodide 构建的,唯一限制是那些依赖 Python 版本的例子可能得不到正确结果。用户体验可能不太好,尤其在手机上(欢迎提改进建议),但目前够用了 :)
satwik查看英文原文
Hi, maintainer of the repo here, thanks for sharing :) If anyone's willing to go through those examples with an interpreter on the side, you can check out https://www.wtfpython.xyz/ It's built with pyiodide, the only limitation is you may not get correct results for the examples that are version-dependent. The UX might not be great, especially on mobile (happy to hear ideas on how to improve it), but it does the job for now :)
可变默认参数只初始化一次,是最容易在真实调试中踩到的坑。
啊对,那条关于可变默认参数的例子我在一次调试中真的被惊到过。Python 在整个脚本运行期间只初始化它们一次,这绝不是任何人会预期到的行为。
alpaca128查看英文原文
Ah yes, the one about mutable default arguments was a real surprise for me once in a debugging session. Python only initialising them once during the whole script runtime is not exactly the behaviour anyone would expect.
反驳:这些 WTF 大多源于刻意写出违背本意的代码,不能算语言缺陷。
我觉得由“做了非常奇怪的事”引发的 WTF,很难算作语言的锅。是的,在括号里用海象运算符确实能跑;它当然会做出奇怪的行为。这真的会让谁吃惊吗?那不是这个运算符的用途。是的,用 "is" 比较字符串有时能用;它本来就不检查相等,也不该检查。换一个实现,用 "is" 可能永远成立也可能永远不成立。别用 "is" 比较字符串,那不是它的用途。是的,把不匹配的操作串起来会产生奇怪结果,写代码时写清楚一点。写出令人困惑的代码得到令人困惑的结果,一点也不奇怪。我最近看到越来越多的反 Python 情绪,大多建立在这类被当成语言缺陷的古怪用法上。Python 绝非完美,但这页上的代码没人会在生产环境里故意写。我想这也说明 Python 越来越流行了。
Doxin查看英文原文
I feel like WTFs caused by doing something deeply weird hardly counts against the language. Yes doing the walrus operator inside brackets works. Yes of course it's going to be doing weird things. Is this really surprising to anyone? It's not what that operator is for. Yes comparing strings with "is" works sometimes. It's not checking equality and it isn't supposed to. In another implementation using "is" might work all the time or none of the time. Don't use "is" to compare strings. it's not what that's for. Yes chaining mismatching operations does weird things. Be clearer in how you write your code. It's no surprise that writing confusing code results in confusing results. I've been seeing a growing anti-python sentiment lately, and most of it seems to be based on these sorts of odd perceived language defects. Python is by no means perfect, but none of the code on this page is something anyone would actually write in production code on purpose. I suppose it's a sign of python getting more popular.
历次讨论
同类还值得读
开源应用架构
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编辑精选