__del__() "Destructor" Method : Instance « Class « Python Tutorial






class C():              
    def __init__(self):  
         print 'initialized'
    def __del__(self):   
         print 'deleted'

c1 = C()               
c2 = c1                
c3 = c1                
print id(c1), id(c2), id(c3) 
del c1                       
del c2                       
del c3                       


  
class InstCt(object):
    count = 0                

    def __init__(self):      
         InstCt.count += 1

    def __del__(self):       
         InstCt.count -= 1

    def howMany(self):       
        return InstCt.count

a = InstCt()
b = InstCt()
print b.howMany()
print a.howMany()
del b
print a.howMany()
del a
InstCt.count








11.10.Instance
11.10.1.Instantiation: Creating Instances by Invoking Class Object
11.10.2.__del__() "Destructor" Method
11.10.3.Determining Instance Attributes
11.10.4.Object interaction
11.10.5.Instance Objects Are Concrete Items