mirror of
https://github.com/satwikkansal/wtfpython
synced 2024-11-22 02:54:25 +01:00
Merge pull request #192 from Jongy/correctify-stubborn-del-explanation
Correctify explanation of "Stubborn `del` operation"
This commit is contained in:
commit
669ff8c30e
8
README.md
vendored
8
README.md
vendored
@ -1954,7 +1954,7 @@ class SomeClass:
|
||||
Deleted!
|
||||
```
|
||||
|
||||
Phew, deleted at last. You might have guessed what saved from `__del__` being called in our first attempt to delete `x`. Let's add more twists to the example.
|
||||
Phew, deleted at last. You might have guessed what saved `__del__` from being called in our first attempt to delete `x`. Let's add more twists to the example.
|
||||
|
||||
2\.
|
||||
```py
|
||||
@ -1973,9 +1973,9 @@ Okay, now it's deleted :confused:
|
||||
|
||||
#### 💡 Explanation:
|
||||
+ `del x` doesn’t directly call `x.__del__()`.
|
||||
+ Whenever `del x` is encountered, Python decrements the reference count for `x` by one, and `x.__del__()` when x’s reference count reaches zero.
|
||||
+ In the second output snippet, `y.__del__()` was not called because the previous statement (`>>> y`) in the interactive interpreter created another reference to the same object, thus preventing the reference count from reaching zero when `del y` was encountered.
|
||||
+ Calling `globals` caused the existing reference to be destroyed, and hence we can see "Deleted!" being printed (finally!).
|
||||
+ When `del x` is encountered, Python deletes the name `x` from current scope and decrements by 1 the reference count of the object `x` referenced. `__del__()` is called only when the object's reference count reaches zero.
|
||||
+ In the second output snippet, `__del__()` was not called because the previous statement (`>>> y`) in the interactive interpreter created another reference to the same object (specifically, the `_` magic variable which references the result value of the last non `None` expression on the REPL), thus preventing the reference count from reaching zero when `del y` was encountered.
|
||||
+ Calling `globals` (or really, executing anything that will have a non `None` result) caused `_` to reference the new result, dropping the existing reference. Now the reference count reached 0 and we can see "Deleted!" being printed (finally!).
|
||||
|
||||
---
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user