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
Copy file name to clipboardExpand all lines: README.md
+45-8Lines changed: 45 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ Python, being a beautifully designed high-level and interpreter-based programmin
10
10
11
11
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!
12
12
13
-
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!
13
+
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 believe that you'll find it interesting too!
14
14
15
15
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:
16
16
@@ -104,10 +104,10 @@ All the examples are structured like below:
104
104
># Preparation for the magic...
105
105
>```
106
106
>
107
-
>**Output (Python version):**
107
+
>**Output (Python version(s)):**
108
108
>```py
109
109
>>>> triggering_statement
110
-
>Probably unexpected output
110
+
>Some unexpected output
111
111
>```
112
112
> (Optional): One line describing the unexpected output.
113
113
>
@@ -118,7 +118,7 @@ All the examples are structured like below:
118
118
>```py
119
119
> Setting up examples for clarification (if necessary)
120
120
>```
121
-
>**Output:**
121
+
>**Output Output (Python version(s)):**
122
122
>```py
123
123
>>>> trigger # some example that makes it easy to unveil the magic
124
124
># some justified output
@@ -128,15 +128,15 @@ All the examples are structured like below:
128
128
129
129
# Usage
130
130
131
-
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:
131
+
A nice way to get the most out of these examples, in my opinion, will be to just read them chronologically, andfor every example:
132
132
- 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.
133
133
- Read the output snippets and,
134
134
+ Check if the outputs are the same as you'd expect.
135
135
+ Make sure if you know the exact reason behind the output being the way it is.
136
-
- 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)).
136
+
- If the answer is no (which is perfectly okay), 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)).
137
137
- If yes, give a gentle pat on your back, and you may skip to the next example.
138
138
139
-
PS: You can also read WTFpython at the command line. There's a pypi package and an npm package (supports colored formatting) for the same.
139
+
PS: You can also read WTFPython at the command line. There's a pypi package and an npm package (which supports colored formatting) for the same.
140
140
141
141
To install the npm package [`wtfpython`](https://www.npmjs.com/package/wtfpython)
142
142
```sh
@@ -2185,7 +2185,10 @@ class SomeClass:
2185
2185
2186
2186
---
2187
2187
2188
-
### ▶ Needle in a Haystack
2188
+
2189
+
---
2190
+
2191
+
### ▶ Needles in a Haystack
2189
2192
2190
2193
1\.
2191
2194
```py
@@ -2224,13 +2227,45 @@ e
2224
2227
tuple()
2225
2228
```
2226
2229
2230
+
3\. Not asserting strongly enough
2231
+
2232
+
```py
2233
+
a = "python"
2234
+
b = "javascript"
2235
+
```
2236
+
**Output:**
2237
+
```py
2238
+
# An assert statement with an assertion failure message.
2239
+
>>> assert(a == b, "Both languages are different")
2240
+
# No AssertionError is raised
2241
+
```
2242
+
2227
2243
#### 💡 Explanation:
2228
2244
* For 1, the correct statement for expected behavior is `x, y = (0, 1) if True else (None, None)`.
2229
2245
* For 2, the correct statement for expected behavior is `t = ('one',)` or `t = 'one',` (missing comma) otherwise the interpreter considers `t` to be a `str` and iterates over it character by character.
2230
2246
* `()` is a special token and denotes empty `tuple`.
2247
+
* For 3, no `AssertionError` was raised because we're asserting entire tuple, instead of asserting the individual expression `a == b`. The following snippet will clear things up,
2248
+
```py
2249
+
>>> a = "python"
2250
+
>>> b = "javascript"
2251
+
>>> assert a == b
2252
+
Traceback (most recent call last):
2253
+
File "<stdin>", line 1, in <module>
2254
+
AssertionError
2255
+
2256
+
>>> assert (a == b, "Values are not equal")
2257
+
<stdin>:1: SyntaxWarning: assertion is always true, perhaps remove parentheses?
2258
+
2259
+
>>> assert a == b, "Values are not equal"
2260
+
Traceback (most recent call last):
2261
+
File "<stdin>", line 1, in <module>
2262
+
AssertionError: Values aren not equal
2263
+
```
2231
2264
2232
2265
---
2233
2266
2267
+
2268
+
2234
2269
---
2235
2270
2236
2271
@@ -2658,6 +2693,8 @@ Let's increase the number of iterations by a factor of 10.
2658
2693
>>> %timeit -n100 add_string_with_plus(10000) # Quadratic increase in execution time
2659
2694
9 ms ± 298 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
2660
2695
```
2696
+
- So many ways to format and create a giant string are somewhat in contrast to the [Zen of Python](https://www.python.org/dev/peps/pep-0020/), according to which,
2697
+
> There should be one-- and preferably only one --obvious way to do it.
0 commit comments