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)
@@ -1376,7 +1376,7 @@ for i in x:
1376
1376
1377
1377
---
1378
1378
1379
-
### > Stubborn `del` operator *
1379
+
### > Stubborn `del` operator/坚强的 `del` *
1380
1380
1381
1381
```py
1382
1382
class SomeClass:
@@ -1389,33 +1389,34 @@ class SomeClass:
1389
1389
```py
1390
1390
>>> x = SomeClass()
1391
1391
>>> y = x
1392
-
>>> del x # this should print "Deleted!"
1392
+
>>> del x # 这里应该会输出 "Deleted!"
1393
1393
>>> del y
1394
1394
Deleted!
1395
1395
```
1396
1396
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.
+ 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!).
0 commit comments