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
+48-39Lines changed: 48 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
<h1align="center"> What the f*ck Python? 🐍 </h1>
2
-
<palign="center"> A collection of interesting and tricky Python examples. </p>
2
+
<palign="center"> An interesting collection of subtle and tricky Python Snippets. </p>
3
3
4
4
[![WTFPL 2.0][license-image]][license-url]
5
5
@@ -10,7 +10,7 @@ Here is a fun project attempting to collect such classic and tricky examples of
10
10
11
11
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!
12
12
13
-
If you're an experienced Python programmer, you might be familiar with most of these examples, and I might be able to revive some sweet old memories of yours being bitten by these gotchas :sweat_smile:
13
+
If you're an experienced Python programmer, you might be 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:
14
14
15
15
So, here ya go...
16
16
@@ -35,92 +35,92 @@ So, here ya go...
35
35
-[💡 Explanation:](#-explanation-1)
36
36
-[Backslashes at the end of string](#backslashes-at-the-end-of-string)
37
37
-[💡 Explanation](#-explanation-4)
38
-
-[String interning](#string-interning)
38
+
-[Strings can be tricky sometimes](#strings-can-be-tricky-sometimes)
39
39
-[💡 Explanation:](#-explanation-2)
40
+
-[`+=` is faster](#-is-faster)
41
+
-[💡 Explanation:](#-explanation-3)
40
42
-[Let's make a giant string!](#lets-make-a-giant-string)
41
43
-[💡 Explanation](#-explanation-5)
42
44
-[Yes, it exists!](#yes-it-exists)
43
-
-[💡 Explanation:](#-explanation-3)
44
-
-[`is` is not what it is!](#is-is-not-what-it-is)
45
45
-[💡 Explanation:](#-explanation-4)
46
+
-[`is` is not what it is!](#is-is-not-what-it-is)
47
+
-[💡 Explanation:](#-explanation-5)
46
48
-[`is not ...` is not `is (not ...)`](#is-not--is-not-is-not-)
47
49
-[💡 Explanation](#-explanation-6)
48
50
-[The function inside loop sticks to the same output](#the-function-inside-loop-sticks-to-the-same-output)
49
51
-[💡 Explanation](#-explanation-7)
50
52
-[Loop variables leaking out of local scope!](#loop-variables-leaking-out-of-local-scope)
51
-
-[💡 Explanation:](#-explanation-5)
52
-
-[A tic-tac-toe where X wins in the first attempt!](#a-tic-tac-toe-where-x-wins-in-the-first-attempt)
53
53
-[💡 Explanation:](#-explanation-6)
54
-
-[Beware of default mutable arguments!](#beware-of-default-mutable-arguments)
54
+
-[A tic-tac-toe where X wins in the first attempt!](#a-tic-tac-toe-where-x-wins-in-the-first-attempt)
55
55
-[💡 Explanation:](#-explanation-7)
56
-
-[Same operands, different story!](#same-operands-different-story)
56
+
-[Beware of default mutable arguments!](#beware-of-default-mutable-arguments)
57
57
-[💡 Explanation:](#-explanation-8)
58
-
-[Mutating the immutable!](#mutating-the-immutable)
58
+
-[Same operands, different story!](#same-operands-different-story)
59
59
-[💡 Explanation:](#-explanation-9)
60
-
-[Using a variable not defined in scope](#using-a-variable-not-defined-in-scope)
60
+
-[Mutating the immutable!](#mutating-the-immutable)
61
61
-[💡 Explanation:](#-explanation-10)
62
-
-[The disappearing variable from outer scope](#the-disappearing-variable-from-outer-scope)
62
+
-[Using a variable not defined in scope](#using-a-variable-not-defined-in-scope)
-[Let's see if you can guess this?](#lets-see-if-you-can-guess-this)
111
+
-[💡 Explanation:](#-explanation-32)
110
112
-[Minor Ones](#minor-ones)
111
113
-[TODO: Hell of an example!](#todo-hell-of-an-example)
112
114
-[Contributing](#contributing)
113
115
-[Acknowledgements](#acknowledgements)
114
116
- [Some nice Links!](#some-nice-links)
115
117
-[🎓 License](#-license)
116
-
-[Donation](#donation)
118
+
-[Help](#help)
117
119
118
120
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
119
121
120
122
# Structure of the Examples
121
123
122
-
**Note:** All the examples mentioned below are tested on Python 3.5.2 interactive interpreter, and they should work for all the Python versions unless explicitly specified in the example description.
123
-
124
124
All the examples are structured like below:
125
125
126
126
> ### Some fancy Title
@@ -150,6 +150,7 @@ All the examples are structured like below:
150
150
># some justified output
151
151
>```
152
152
153
+
**Note:** All the examples mentioned below are tested on Python 3.5.2 interactive interpreter, and they should work forall the Python versions unless explicitly specified in the example description.
153
154
154
155
# Usage
155
156
@@ -167,7 +168,7 @@ $ npm install -g wtfpython
167
168
```
168
169
Now, just run `wtfpython` at the command line which will open this collection in your selected `$PAGER`.
169
170
170
-
#TODO: Add pypi package for reading via command line
171
+
#TODO: Add pypi package for reading via command line (yes, this is the real wtf for now!)
171
172
172
173
# 👀 Examples
173
174
@@ -445,13 +446,18 @@ True
445
446
>>>b="wtf!"
446
447
>>> a is b
447
448
False
449
+
450
+
>>> a, b="wtf!", "wtf!"
451
+
>>> a is b
452
+
True
448
453
```
449
454
450
455
3\.
451
456
```py
452
457
>>>'a'*20is'aaaaaaaaaaaaaaaaaaaa'
453
458
True
454
459
>>>'a'*21is'aaaaaaaaaaaaaaaaaaaaa'
460
+
False
455
461
```
456
462
457
463
Makes sense, right?
@@ -463,6 +469,7 @@ Makes sense, right?
463
469
* All length 0and length 1 strings are interned.
464
470
* Strings are interned at compile time (`'wtf'` will be interned but `''.join(['w', 't', 'f']` will not be interned)
465
471
* Strings that are not composed of ascii letters, digits or underscores, are not interned. This explains why `'wtf!'` was not interned due to `!`.
472
+
+ When `a`and`b` are set to `"wtf!"`in the same line, the Python interpreter creates a new object, then references the second variable at the same time. If you do it on separate lines, it doesn't "know" that there's already `wtf!`as an object (because `"wtf!"`isnot implicitly interned as per the facts mentioned above). It's a compiler optimization and specifically applies to the interactive environment.
466
473
467
474
---
468
475
@@ -2169,10 +2176,12 @@ The idea and design for this collection are inspired by Denys Dovhan's awesome p
This project costed me nothing and doesn't require anything to operate. However, I'm looking for full-time opportunities, I'd **highly appreciate** if you can let me know about open positions around you.
2181
+
Hey, I'm looking for full-time opportunities, I'd highly appreciate if you could do me a small favor by letting me know about open positions around you.
2175
2182
2176
-
And finally, Thanks for reading this project, I hope you enjoyed it and found it informative!
2183
+
And finally, Thanks a ton for reading this project, I hope you enjoyed it and found it informative!
2177
2184
2185
+
Want to share What the f**k Python with friends?
2178
2186
2187
+
[Twitter](https://twitter.com/intent/tweet?url=https://github.com/satwikkansal/wtfpython&hastags=python,wtfpython) | [Linkedin](https://www.linkedin.com/shareArticle?url=https://github.com/satwikkansal&title=What the f*ck Python!&summary=An interesting collection of subtle and tricky Python snippets.)
0 commit comments