_del_ () method

Source: Internet
Author: User

After the object is created, the Python interpreter calls the _init_ () method by default;

When an object is deleted, the Python interpreter also calls a method by default, which is the _del_ () method

Import TimeclassAnimal (object):#Construction Method    def_init_ (self,name):Print("The _init_ method is called") Self._name=name#Method     def_del_ (self):Print("The _del_ method is called")         Print("%s object is going to be wiped out soon ... "%self._name)#Creating ObjectsDog =Animal ("Hello dog")#Delete ObjectdelDogcat=Animal ("Persian cat") Cat2=CATCAT3=CatPrint("----Delete Cat objects immediately")delCatPrint("----Delete the Cat2 object immediately")delCat2Print("---Delete the Cat3 object immediately")delCAT3Print("program ends after 2 seconds") Time.sleep (2)

Summarize:

* When a class holds a reference to an object, the reference count of this object increases by 1

* When using Del to delete the object that the variable points to, if the object's reference count is not 1, such as 3, then it will only let the user reference count minus 1, that is, to 2, when the Del is called again, 1, if the call to 1 del, this will actually delete the object.

_del_ () method

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.