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
+58Lines changed: 58 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -83,6 +83,8 @@ So, here ya go...
83
83
-[💡 Explanation:](#-explanation-16)
84
84
-[Needle in a Haystack](#needle-in-a-haystack)
85
85
-[💡 Explanation:](#-explanation-17)
86
+
-[Let's see if you can guess this?](#lets-see-if-you-can-guess-this)
87
+
-[💡 Explanation:](#-explanation-18)
86
88
-[Minor Ones](#minor-ones)
87
89
-[TODO: Hell of an example!](#todo-hell-of-an-example)
88
90
-[Contributing](#contributing)
@@ -1396,6 +1398,62 @@ tuple()
1396
1398
1397
1399
---
1398
1400
1401
+
### Let's see if you can guess this?
1402
+
1403
+
Originally, suggested by @PiaFraus in [this](https://github.com/satwikkansal/wtfPython/issues/9) issue.
1404
+
1405
+
```py
1406
+
a, b= a[b] = {}, 5
1407
+
```
1408
+
1409
+
**Output:**
1410
+
```py
1411
+
>>> a
1412
+
{5: ({...}, 5)}
1413
+
```
1414
+
1415
+
#### 💡 Explanation:
1416
+
1417
+
* According to [Python language reference](https://docs.python.org/2/reference/simple_stmts.html#assignment-statements), assignment statements have the form
> An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right.
1423
+
1424
+
* The `+`in`(target_list "=")+` means there can be **one or more** target lists. In this case, target lists are `a, b`and`a[b]` (note the expression listis exactly one, which in our case is`{}, 5`).
1425
+
1426
+
* After the expression listis evaluated, it's value is unpacked to the target lists from **left to right**. So, in our case, first the `{}, 5` tuple is unpacked to `a, b` and we now have `a = {}` and `b = 5`.
1427
+
1428
+
*`a`is now assigned to `{}` which is a mutable object.
1429
+
1430
+
* The second target listis`a[b]` (you may expect this to throw an error because both `a`and`b` have not been defined in the statements before. But remember, we just assigned `a` to `{}`and`b` to `5`).
1431
+
1432
+
* Now, we are setting the key `5`in the dictionary to the tuple`({}, 5)` creating a circular reference (the `{...}`in the output refers to the same object that `a`is already referencing). Another simpler example of circular reference could be
1433
+
```py
1434
+
>>>some_list= some_list[0] = [0]
1435
+
>>> some_list
1436
+
[[...]]
1437
+
>>> some_list[0]
1438
+
[[...]]
1439
+
>>> some_list is some_list[0]
1440
+
[[...]]
1441
+
```
1442
+
Similar is the case in our example (`a[b][0]`is the same objectas`a`)
1443
+
1444
+
* So to sum it up, you can break the example down to
1445
+
```py
1446
+
a, b= {}, 5
1447
+
a[b] = a, b
1448
+
```
1449
+
And the circular reference can be justified by the fact that `a[b][0]`is the same objectas`a`
1450
+
```py
1451
+
>>> a[b][0] is a
1452
+
True
1453
+
```
1454
+
1455
+
---
1456
+
1399
1457
### Minor Ones
1400
1458
1401
1459
*`join()`is a string operation instead of list operation. (sort of counter-intuitive at first usage)
0 commit comments