Skip to content

Commit c6e0d79

Browse files
committed
Some minor changes, and add assertion example
1 parent 362447a commit c6e0d79

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

README.md

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Python, being a beautifully designed high-level and interpreter-based programmin
1010

1111
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!
1212

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!
1414

1515
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:
1616

@@ -104,10 +104,10 @@ All the examples are structured like below:
104104
> # Preparation for the magic...
105105
> ```
106106
>
107-
> **Output (Python version):**
107+
> **Output (Python version(s)):**
108108
> ```py
109109
> >>> triggering_statement
110-
> Probably unexpected output
110+
> Some unexpected output
111111
> ```
112112
> (Optional): One line describing the unexpected output.
113113
>
@@ -118,7 +118,7 @@ All the examples are structured like below:
118118
> ```py
119119
> Setting up examples for clarification (if necessary)
120120
> ```
121-
> **Output:**
121+
> **Output Output (Python version(s)):**
122122
> ```py
123123
> >>> trigger # some example that makes it easy to unveil the magic
124124
> # some justified output
@@ -128,15 +128,15 @@ All the examples are structured like below:
128128
129129
# Usage
130130
131-
A nice way to get the most out of these examples, in my opinion, will be just to read the examples chronologically, and for every example:
131+
A nice way to get the most out of these examples, in my opinion, will be to just read them chronologically, and for every example:
132132
- 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.
133133
- Read the output snippets and,
134134
+ Check if the outputs are the same as you'd expect.
135135
+ 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 (and if 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 (and if you still don't understand, shout out! and create an issue [here](https://github.com/satwikkansal/wtfPython)).
137137
- If yes, give a gentle pat on your back, and you may skip to the next example.
138138
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.
140140
141141
To install the npm package [`wtfpython`](https://www.npmjs.com/package/wtfpython)
142142
```sh
@@ -2185,7 +2185,10 @@ class SomeClass:
21852185
21862186
---
21872187
2188-
### ▶ Needle in a Haystack
2188+
2189+
---
2190+
2191+
### ▶ Needles in a Haystack
21892192
21902193
1\.
21912194
```py
@@ -2224,13 +2227,45 @@ e
22242227
tuple()
22252228
```
22262229
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+
22272243
#### 💡 Explanation:
22282244
* For 1, the correct statement for expected behavior is `x, y = (0, 1) if True else (None, None)`.
22292245
* 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.
22302246
* `()` 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+
```
22312264
22322265
---
22332266
2267+
2268+
22342269
---
22352270
22362271
@@ -2658,6 +2693,8 @@ Let's increase the number of iterations by a factor of 10.
26582693
>>> %timeit -n100 add_string_with_plus(10000) # Quadratic increase in execution time
26592694
9 ms ± 298 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
26602695
```
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.
26612698
26622699
---
26632700

0 commit comments

Comments
 (0)