Skip to content

Commit f1c2b68

Browse files
committed
Add new example: The stubborn del operator
Closes #26
1 parent e94cbea commit f1c2b68

1 file changed

Lines changed: 50 additions & 1 deletion

File tree

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ So, here ya go...
103103
- [💡 Explanation:](#-explanation-28)
104104
- [Implicity key type conversion](#implicity-key-type-conversion)
105105
- [💡 Explanation:](#-explanation-29)
106-
- [Let's see if you can guess this?](#lets-see-if-you-can-guess-this)
106+
- [Stubborn `del` operator](#stubborn-del-operator)
107107
- [💡 Explanation:](#-explanation-30)
108+
- [Let's see if you can guess this?](#lets-see-if-you-can-guess-this)
109+
- [💡 Explanation:](#-explanation-31)
108110
- [Minor Ones](#minor-ones)
109111
- [TODO: Hell of an example!](#todo-hell-of-an-example)
110112
- [Contributing](#contributing)
@@ -1931,6 +1933,53 @@ str
19311933
(__main__.SomeClass, str)
19321934
```
19331935
1936+
---
1937+
1938+
### Stubborn `del` operator
1939+
1940+
Suggested by @tukkek in [this](https://github.com/satwikkansal/wtfpython/issues/26) issue.
1941+
1942+
```py
1943+
class SomeClass:
1944+
def __del__(self):
1945+
print("Deleted!")
1946+
```
1947+
1948+
**Output:**
1949+
1\.
1950+
```py
1951+
>>> x = SomeClass()
1952+
>>> y = x
1953+
>>> del x # this should print "Deleted!"
1954+
>>> del y
1955+
Deleted!
1956+
```
1957+
1958+
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 twist ro the example.
1959+
1960+
2\.
1961+
```py
1962+
>>> x = SomeClass()
1963+
>>> y = x
1964+
>>> del x
1965+
>>> y # check if y exists
1966+
<__main__.SomeClass instance at 0x7f98a1a67fc8>
1967+
>>> del y # Like previously, this should print "Deleted!"
1968+
>>> globals() # oh, it didn't. Let's check all our global variables and confirm
1969+
Deleted!
1970+
{'__builtins__': <module '__builtin__' (built-in)>, 'SomeClass': <class __main__.SomeClass at 0x7f98a1a5f668>, '__package__': None, '__name__': '__main__', '__doc__': None}
1971+
```
1972+
1973+
Okay, now it's deleted :confused:
1974+
1975+
#### 💡 Explanation:
1976+
+ `del x` doesn’t directly call `x.__del__()`.
1977+
+ Whenever `del x` is encountered, Python decrements the reference count for `x` by one, and `x.__del__()` when x’s reference count reaches zero.
1978+
+ 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 to reach zero when `del y` was encountered.
1979+
+ Calling `globals` caused the existing reference to be destroyed and hence we can see "Deleted!" being printed (finally!).
1980+
1981+
---
1982+
19341983
### Let's see if you can guess this?
19351984
19361985
Suggested by @PiaFraus in [this](https://github.com/satwikkansal/wtfPython/issues/9) issue.

0 commit comments

Comments
 (0)