You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[> Beware of default mutable arguments!](#-beware-of-default-mutable-arguments)
@@ -1381,7 +1381,7 @@ for i in x:
1381
1381
1382
1382
---
1383
1383
1384
-
### > Stubborn `del` operator *
1384
+
### > Stubborn `del` operator/坚强的 `del` *
1385
1385
1386
1386
```py
1387
1387
class SomeClass:
@@ -1394,33 +1394,34 @@ class SomeClass:
1394
1394
```py
1395
1395
>>> x = SomeClass()
1396
1396
>>> y = x
1397
-
>>> del x # this should print "Deleted!"
1397
+
>>> del x # 这里应该会输出 "Deleted!"
1398
1398
>>> del y
1399
1399
Deleted!
1400
1400
```
1401
1401
1402
-
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.
+ Whenever `del x` is encountered, Python decrements the reference count for `x` by one, and `x.__del__()` when x’s reference count reaches zero.
1422
-
+ 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.
1423
-
+ Calling `globals` caused the existing reference to be destroyed and hence we can see "Deleted!" being printed (finally!).
0 commit comments