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