Skip to content

Commit 850a968

Browse files
暮晨ducheng
暮晨
authored and
ducheng
committed
EX.Stubborn del operator
1 parent ea1b055 commit 850a968

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ So, here we go...
5656
- [> Well, something is fishy.../嗯, 有些可疑...](#-well-something-is-fishy嗯有些可疑)
5757
- [Section: Watch out for the landmines!](#section-watch-out-for-the-landmines)
5858
- [> Modifying a dictionary while iterating over it/迭代字典时的修改](#-modifying-a-dictionary-while-iterating-over-it迭代字典时的修改)
59-
- [> Stubborn `del` operator *](#-stubborn-del-operator-)
59+
- [> Stubborn `del` operator/坚强的 `del` *](#-stubborn-del-operator坚强的-del-)
6060
- [> Deleting a list item while iterating](#-deleting-a-list-item-while-iterating)
6161
- [> Loop variables leaking out!](#-loop-variables-leaking-out)
6262
- [> Beware of default mutable arguments!](#-beware-of-default-mutable-arguments)
@@ -1376,7 +1376,7 @@ for i in x:
13761376
13771377
---
13781378
1379-
### > Stubborn `del` operator *
1379+
### > Stubborn `del` operator/坚强的 `del` *
13801380
13811381
```py
13821382
class SomeClass:
@@ -1389,33 +1389,34 @@ class SomeClass:
13891389
```py
13901390
>>> x = SomeClass()
13911391
>>> y = x
1392-
>>> del x # this should print "Deleted!"
1392+
>>> del x # 这里应该会输出 "Deleted!"
13931393
>>> del y
13941394
Deleted!
13951395
```
13961396
1397-
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 to the example.
1397+
唷, 终于删除了. 你可能已经猜到了在我们第一次尝试删除 `x` 时是什么让 `__del__` 免于被调用的. 那让我们给这个例子增加点难度.
13981398
13991399
2\.
14001400
```py
14011401
>>> x = SomeClass()
14021402
>>> y = x
14031403
>>> del x
1404-
>>> y # check if y exists
1404+
>>> y # 检查一下y是否存在
14051405
<__main__.SomeClass instance at 0x7f98a1a67fc8>
1406-
>>> del y # Like previously, this should print "Deleted!"
1407-
>>> globals() # oh, it didn't. Let's check all our global variables and confirm
1406+
>>> del y # 像之前一样, 这里应该会输出 "Deleted!"
1407+
>>> globals() # 好吧, 并没有. 让我们看一下所有的全局变量
14081408
Deleted!
14091409
{'__builtins__': <module '__builtin__' (built-in)>, 'SomeClass': <class __main__.SomeClass at 0x7f98a1a5f668>, '__package__': None, '__name__': '__main__', '__doc__': None}
14101410
```
14111411
1412-
Okay, now it's deleted :confused:
1412+
好了,现在它被删除了 :confused:
14131413
1414-
#### 💡 Explanation:
1415-
+ `del x` doesn’t directly call `x.__del__()`.
1416-
+ Whenever `del x` is encountered, Python decrements the reference count for `x` by one, and `x.__del__()` when x’s reference count reaches zero.
1417-
+ 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.
1418-
+ Calling `globals` caused the existing reference to be destroyed and hence we can see "Deleted!" being printed (finally!).
1414+
#### 💡 说明:
1415+
+ `del x` 并不会立刻调用 `x.__del__()`.
1416+
+ 每当遇到 `del x`, Python 会将 `x` 的引用数减1, 当 `x` 的引用数减到0时就会调用 `x.__del__()`.
1417+
+ 在第二个例子中, `y.__del__()` 之所以未被调用, 是因为前一条语句 (`>>> y`) 对同一对象创建了另一个引用, 从而防止在执行 `del y` 后对象的引用数变为0.
1418+
+ 调用 `globals` 导致引用被销毁, 因此我们可以看到 "Deleted!" 终于被输出了.
1419+
+ (译: 这其实是 Python 交互解释器的特性, 它会自动让 `_` 保存上一个表达式输出的值, 详细可以看[这里](https://www.cnblogs.com/leisurelylicht/p/diao-pi-de-kong-zhi-tai.html).)
14191420
14201421
---
14211422

0 commit comments

Comments
 (0)