You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python, being a beautifully designed high-level and interpreter-based programming language, provides us with many features for the programmer's comfort. But sometimes, the outcomes of a Python snippet may not seem obvious to a regular user at first sight.
Here is a fun project to collect such tricky & counter-intuitive examples and lesser-known features in Python, attempting to discuss what exactly is happening under the hood!
While some of the examples you see below may not be WTFs in the truest sense, but they'll reveal some of the interesting parts of Python that you might be unaware of. I find it a nice way to learn the internals of a programming language, and I think you'll find them interesting as well!
If you're an experienced Python programmer, you can take it as a challenge to get most of them right in first attempt. You may be already familiar with some of these examples, and I might be able to revive sweet old memories of yours being bitten by these gotchas:sweat_smile:
-[> Let's make a giant string!/来做个巨大的字符串吧!](#-lets-make-a-giant-string来做个巨大的字符串吧)
81
86
-[> Explicit typecast of strings/字符串的显式类型转换](#-explicit-typecast-of-strings字符串的显式类型转换)
82
87
-[> Minor Ones/小知识点](#-minor-ones小知识点)
83
-
-[Contributing](#contributing)
84
-
-[Acknowledgements](#acknowledgements)
85
-
-[🎓 License](#🎓-license)
86
-
-[Help](#help)
87
-
-[Want to surprise your geeky pythonist friends?](#want-to-surprise-your-geeky-pythonist-friends)
88
-
-[Need a pdf version?](#need-a-pdf-version)
89
-
-[Follow Commit](#follow-commit)
88
+
-[Contributing/贡献](#contributing贡献)
89
+
-[Acknowledgements/致谢](#acknowledgements致谢)
90
+
-[🎓 License/许可](#-license许可)
91
+
-[Help/帮助](#help帮助)
92
+
-[Surprise your geeky pythonist friends?/想给你的极客朋友一个惊喜?](#surprise-your-geeky-pythonist-friends想给你的极客朋友一个惊喜)
93
+
-[Need a pdf version?/需要来一份pdf版的?](#need-a-pdf-version需要来一份pdf版的)
94
+
-[Follow Commit/追踪Commit](#follow-commit追踪Commit)
90
95
91
96
<!-- /TOC -->
92
97
93
-
# Structure of the Examples
98
+
# Structure of the Examples/示例结构
94
99
95
-
All the examples are structured like below:
100
+
所有示例的结构都如下所示:
96
101
97
-
> ### > Some fancy Title *
98
-
> The asterisk at the end of the title indicates the example was not present in the first release and has been recently added.
102
+
> ### > 一个精选的标题 *
103
+
> 标题末尾的星号表示该示例在第一版中不存在,是最近添加的.
99
104
>
100
105
> ```py
101
-
>#Setting up the code.
102
-
>#Preparation for the magic...
106
+
>#准备代码.
107
+
>#释放魔法...
103
108
>```
104
109
>
105
110
>**Output (Python version):**
106
111
>```py
107
-
>>>>triggering_statement
108
-
>Probably unexpected output
112
+
>>>>触发语句
113
+
>出乎意料的输出结果
109
114
>```
110
-
> (Optional): One line describing the unexpected output.
115
+
> (可选): 对意外输出结果的简短描述.
111
116
>
112
117
>
113
-
>#### 💡 Explanation:
118
+
>#### 💡 说明:
114
119
>
115
-
>*Brief explanation of what's happening and why is it happening.
120
+
>*简要说明发生了什么以及为什么会发生.
116
121
>```py
117
-
>Setting up examples for clarification (if necessary)
122
+
>如有必要, 举例说明
118
123
>```
119
124
>**Output:**
120
125
>```py
121
-
>>>>trigger#some example that makes it easy to unveil the magic
122
-
>#some justified output
126
+
>>>>触发语句#一些让魔法变得容易理解的例子
127
+
>#一些正常的输入
123
128
>```
124
129
125
-
**Note:**All the examples are tested on Python 3.5.2 interactive interpreter, and they should work forall the Python versions unless explicitly specified in the description.
A nice way to get the most out of these examples, in my opinion, will be just to read the examples chronologically, andfor every example:
130
-
-Carefully read the initial code for setting up the example. If you're an experienced Python programmer, most of the times you will successfully anticipate what's going to happen next.
131
-
-Read the output snippets and,
132
-
+Check if the outputs are the same as you'd expect.
133
-
+Make sure if you know the exact reason behind the output being the way it is.
134
-
-If no, take a deep breath, and read the explanation (andif you still don't understand, shout out! and create an issue [here](https://github.com/satwikkansal/wtfPython)).
135
-
-If yes, give a gentle pat on your back, and you may skip to the next example.
For discussions, you can either create a new [issue](https://github.com/satwikkansal/wtfpython/issues/new) or ping on the Gitter [channel](https://gitter.im/wtfpython/Lobby)
The idea and design for this collection were initially inspired by Denys Dovhan's awesome project [wtfjs](https://github.com/denysdovhan/wtfjs). The overwhelming support by the community gave it the shape it is in right now.
I've received a few requests for the pdf version of wtfpython. You can add your details [here](https://satwikkansal.xyz/wtfpython-pdf/) to get the pdf as soon as it is finished.
0 commit comments