From 67f743eddd8cbc5e55bda08d604c938ee92a3c42 Mon Sep 17 00:00:00 2001 From: Yonatan Goldschmidt Date: Tue, 24 Mar 2020 22:22:34 +0200 Subject: [PATCH] Correctify explanation of Stubborn `del` operation --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2702a66..4d76ec2 100644 --- a/README.md +++ b/README.md @@ -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!). ---