Question

I can't invoke the destructor of the Parent class from the destructor of the Child class. Please, check the code below:

class BaseClass(object):
    def __del__(self):
        print 'BaseClass->Destructor'

class DerivativeClass(BaseClass):
    def __del__(self):
        print 'DerivativeClass->Destructor'
        #Invoke destructor of the base class, it works 
        BaseClass.__del__(self)           
        #Doesn't work
        #super(DerivativeClass, self).__del__() 

instance = DerivativeClass()

When I use super(DerivativeClass, self).__del__() in DerivativeClass.__del__() I got the following error:

Exception TypeError: 'must be type, not None' in <bound method 
DerivativeClass.__del__ of <__main__.DerivativeClass object at 0xb73a682c>> ignored

QUESTION:

Why can't I use super in the destructor of the child class while it's perfectly fine in the constructor of the child class?

Was it helpful?

Solution

It seems that this happens only on runtime exit (because DerivativeClass is None).

This works fine:

class BaseClass(object):
    def __del__(self):
        print 'BaseClass->Destructor'

class DerivativeClass(BaseClass):
    def __del__(self):
        print 'DerivativeClass->Destructor'
        super(DerivativeClass, self).__del__() 

instance = DerivativeClass()
del instance

And this works fine too:

class BaseClass(object):
    def __del__(self):
        print 'BaseClass->Destructor'

class DerivativeClass(BaseClass):
    def __del__(self):
        print 'DerivativeClass->Destructor'
        super(type(self), self).__del__() 

instance = DerivativeClass()

Also this:

class BaseClass(object):
    def __del__(self):
        print 'BaseClass->Destructor'

class DerivativeClass(BaseClass):
    def __del__(self):
        print 'DerivativeClass->Destructor'
        super(DerivativeClass, self).__del__() 

def someScope ():
    instance = DerivativeClass()

someScope ()

In snippets 1 and 3, I make sure that the instance dies before the class. In snippet 2 I circumnavigate the whole issue in quite an ugly manner.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top