Discussing the keyword del and instance method _ del _ (self)

Source: Internet
Author: User
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 >>>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.