Python has many special methods like _ del _ (self), such as _ gt _ () and _ le __() you can guess the approximate function by name, but some cannot guess it like this (or, of course, only one of me will ), for example, if _ del _ () and del are coming soon, I thought that when del is an object, __del _ () will be called, however, after a failed written examination, I began to review these details. ^_^! Talk less... python has many special methods like _ del _ (self), such as _ gt _ () and _ le __() you can guess the approximate function by name, but some cannot guess it like this (or, of course, only one of me will ), for example, if _ del _ () and del are coming soon, I thought that when del is an object, __del _ () will be called, however, after a failed written examination, I began to review these details. ^_^!
Enter the topic if you want to talk less!
We know that del is used to delete an object. to be precise, it should be to reduce the reference count of this object by one. But note that it is not called every time, __del _ () will be called, because _ del __() it is called only when the reference count of this object is reduced to 0. for example:
>>> Class C: def _ del _ (self): print ("I'm vanishing... ") >>> c = C () >>> B = c # note that the reference count for C () is 2 >>> del B >>># look, no output is provided. the description _ del _ () is not called >>> del cI'm vanishing... # call after the reference count is reduced to 0 >>>