1
0
mirror of https://github.com/satwikkansal/wtfpython synced 2024-11-25 20:44:24 +01:00

Add new example: The stubborn del operator

Closes https://github.com/satwikkansal/wtfpython/issues/26
This commit is contained in:
Satwik Kansal 2018-01-11 14:12:44 +05:30
parent e94cbea6bc
commit f1c2b68d32

51
README.md vendored
View File

@ -103,8 +103,10 @@ So, here ya go...
- [💡 Explanation:](#-explanation-28) - [💡 Explanation:](#-explanation-28)
- [Implicity key type conversion](#implicity-key-type-conversion) - [Implicity key type conversion](#implicity-key-type-conversion)
- [💡 Explanation:](#-explanation-29) - [💡 Explanation:](#-explanation-29)
- [Let's see if you can guess this?](#lets-see-if-you-can-guess-this) - [Stubborn `del` operator](#stubborn-del-operator)
- [💡 Explanation:](#-explanation-30) - [💡 Explanation:](#-explanation-30)
- [Let's see if you can guess this?](#lets-see-if-you-can-guess-this)
- [💡 Explanation:](#-explanation-31)
- [Minor Ones](#minor-ones) - [Minor Ones](#minor-ones)
- [TODO: Hell of an example!](#todo-hell-of-an-example) - [TODO: Hell of an example!](#todo-hell-of-an-example)
- [Contributing](#contributing) - [Contributing](#contributing)
@ -1931,6 +1933,53 @@ str
(__main__.SomeClass, str) (__main__.SomeClass, str)
``` ```
---
### Stubborn `del` operator
Suggested by @tukkek in [this](https://github.com/satwikkansal/wtfpython/issues/26) issue.
```py
class SomeClass:
def __del__(self):
print("Deleted!")
```
**Output:**
1\.
```py
>>> x = SomeClass()
>>> y = x
>>> del x # this should print "Deleted!"
>>> del y
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 twist ro the example.
2\.
```py
>>> x = SomeClass()
>>> y = x
>>> del x
>>> y # check if y exists
<__main__.SomeClass instance at 0x7f98a1a67fc8>
>>> del y # Like previously, this should print "Deleted!"
>>> globals() # oh, it didn't. Let's check all our global variables and confirm
Deleted!
{'__builtins__': <module '__builtin__' (built-in)>, 'SomeClass': <class __main__.SomeClass at 0x7f98a1a5f668>, '__package__': None, '__name__': '__main__', '__doc__': None}
```
Okay, now it's deleted :confused:
#### 💡 Explanation:
+ `del x` doesnt directly call `x.__del__()`.
+ Whenever `del x` is encountered, Python decrements the reference count for `x` by one, and `x.__del__()` when xs 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 to reach zero when `del y` was encountered.
+ Calling `globals` caused the existing reference to be destroyed and hence we can see "Deleted!" being printed (finally!).
---
### Let's see if you can guess this? ### Let's see if you can guess this?
Suggested by @PiaFraus in [this](https://github.com/satwikkansal/wtfPython/issues/9) issue. Suggested by @PiaFraus in [this](https://github.com/satwikkansal/wtfPython/issues/9) issue.