Discussion of Keyword Del and instance method __del__ (self)

Source: Internet
Author: User
Python has a lot of special methods like __del__ (self), some such as __gt__ (), __le__ () and so on can be guessed from the name of its approximate function, but some can not be so random guessing (of course, it is possible that I alone will do), For example, __del__ () and Del, which I used to think when Del is an object, __del__ () will be called, however, after a few written tests have failed, I began to examine these finer places ^_^!
Gossip less, get to the point!
We know del is deleting an object, in fact, it should be to make the object's reference count minus one. Note, however, that __del__ () is not called every time it is called, because __del__ () is called when the reference count of the object is reduced to 0 o'clock, as the example permits:

>>> class C:        def __del__ (self):        print ("I ' m Vanishing ...") >>> C = C () >>> B = C            # Note , at this point the reference count for C () is 2>>> del b>>>                  # Look, what output is not, the description __del__ () does not call >>> del CI ' m vanishing ...     # The reference count is reduced to 0 before it is called >>>
  • Related Article

    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.