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: wtfpy.md
+57-2Lines changed: 57 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
5
> A collection of tricky Python examples
6
6
7
-
Python being an awesomoe higher level language, provides us many functionalities for our comfort. But sometimes, the outcomes may not seem obvious to a normal Python user at the first sight. Here's an attempt to collect such examples and see what exactly is happening under the hood! I find it a nice way to learn internals of a language and I think you'll like them as well!
7
+
Python being an awesomoe higher level language, provides us many functionalities for our comfort. But sometimes, the outcomes may not seem obvious to a normal Python user at the first sight. Here's an attempt to collect such classic examples of unexpected behaviors in Python and see what exactly is happening under the hood! I find it a nice way to learn internals of a language and I think you'll like them as well!
8
8
9
9
# Table of Contents
10
10
@@ -130,6 +130,8 @@ Quoting from https://docs.python.org/3/c-api/long.html
130
130
140084850247344
131
131
```
132
132
133
+
Here the integer isn't smart enough while executing `y = 257` to recongnize that we've already created an integer of the value `257` and so it goes on to create another object in the memory.
134
+
133
135
134
136
**Both `a` and `b` refer to same object, when initialized with same value in same line**
135
137
@@ -151,7 +153,7 @@ Quoting from https://docs.python.org/3/c-api/long.html
151
153
```
152
154
153
155
154
-
## The loop magic
156
+
## The function inside loop magic
155
157
156
158
```py
157
159
funcs = []
@@ -202,6 +204,59 @@ for x in range(7):
202
204
[0, 1, 2, 3, 4, 5, 6]
203
205
```
204
206
207
+
## A tic-tac-toe where X wins in first attempt!
208
+
209
+
```py
210
+
# Let's initialize a row
211
+
row = [""]*3#row i['', '', '']
212
+
# Let's make a bord
213
+
board = [row]*3
214
+
```
215
+
216
+
**Output:**
217
+
```py
218
+
>>> board
219
+
[['', '', ''], ['', '', ''], ['', '', '']]
220
+
>>> board[0]
221
+
['', '', '']
222
+
>>> board[0][0]
223
+
''
224
+
>>> board[0][0] ="X"
225
+
>>> board
226
+
[['X', '', ''], ['X', '', ''], ['X', '', '']]
227
+
```
228
+
229
+
### Explanation
230
+
231
+
When we initialize `row` varaible, this visualization explains what happens in the memory
And when the `board` is initialized by multiplying the `row`, this is what happens inside the memory (each of the elements board[0], board[1] and board[2] is a reference to the same list referred by `row`)
0 commit comments