mirror of
https://github.com/satwikkansal/wtfpython
synced 2024-11-22 11:04:25 +01:00
Add new example
Closes https://github.com/satwikkansal/wtfPython/issues/9
This commit is contained in:
parent
f76511c15b
commit
8ae68f11b6
53
README.md
vendored
53
README.md
vendored
@ -1396,6 +1396,59 @@ tuple()
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
### Let's see if you can guess this?
|
||||||
|
|
||||||
|
Originally, suggested by @PiaFraus in [this](https://github.com/satwikkansal/wtfPython/issues/9) issue.
|
||||||
|
|
||||||
|
**Output:**
|
||||||
|
```py
|
||||||
|
>>> a, b = a[b] = {}, 5
|
||||||
|
>>> a
|
||||||
|
{5: ({...}, 5)}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 💡 Explanation:
|
||||||
|
|
||||||
|
* According to [Python language reference](https://docs.python.org/2/reference/simple_stmts.html#assignment-statements), assignment statements have the form
|
||||||
|
```
|
||||||
|
(target_list "=")+ (expression_list | yield_expression)
|
||||||
|
```
|
||||||
|
and
|
||||||
|
> 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.
|
||||||
|
|
||||||
|
* 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 list is exactly one, which in our case is `{}, 5`).
|
||||||
|
|
||||||
|
* After the expression list is 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`.
|
||||||
|
|
||||||
|
* `a` is now assigned to `{}` which is a mutable object.
|
||||||
|
|
||||||
|
* The second target list is `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`).
|
||||||
|
|
||||||
|
* 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
|
||||||
|
```py
|
||||||
|
>>> some_list = some_list[0] = [0]
|
||||||
|
>>> some_list
|
||||||
|
[[...]]
|
||||||
|
>>> some_list[0]
|
||||||
|
[[...]]
|
||||||
|
>>> some_list is some_list[0]
|
||||||
|
[[...]]
|
||||||
|
```
|
||||||
|
Similar is the case in our example (`a[b][0]` is the same object as `a`)
|
||||||
|
|
||||||
|
* So to sum it up, you can break the example down to
|
||||||
|
```py
|
||||||
|
a, b = {}, 5
|
||||||
|
a[b] = a, b
|
||||||
|
```
|
||||||
|
And the circular reference can be justified by the fact that `a[b][0]` is the same object as `a`
|
||||||
|
```py
|
||||||
|
>>> a[b][0] is a
|
||||||
|
True
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
### Minor Ones
|
### Minor Ones
|
||||||
|
|
||||||
* `join()` is a string operation instead of list operation. (sort of counter-intuitive at first usage)
|
* `join()` is a string operation instead of list operation. (sort of counter-intuitive at first usage)
|
||||||
|
Loading…
Reference in New Issue
Block a user