Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3, super.__del__()

I have a __del__ method in a class I've defined, to delete some C++ objects created by calling C++ new in a ctypes interface. I want to delete these objects when an instance of my class is destroyed. I have a fragment of the class shown here:

class Graph(QtCore.QObject):
    def __init__(self):
        super().__init__()
        #list of objects created by calls to ctypes to create pointers to C++ objects which are instantiated with C++ new 
        self.graphs = []

    def __del__(self):
        print("in delete method")
        for graph in self.graphs:
            # call the C++ delete to free the storage used by this graph
            myctypes.graphDelete(graph)
        super().__del__()

When an instance of my Graph class is deleted, the __del__ method is called and I see my print statement and when I set a breakpoint in the destructor method in the C++ code, as expected, it deletes the object. However, when my __del__ method calls super().__del__(), I get the error message:

super().__del__()
AttributeError: 'super' object has no attribute '__del__'

How do I ensure that the parent class (QtCore.QObject) is deleted if I define my own __del__ method in the child class or will the parent class be deleted automatically?

like image 426
inwhack Avatar asked Apr 19 '16 15:04

inwhack


People also ask

What does super () __ Init__ do?

Understanding Python super() with __init__() methods It is known as a constructor in Object-Oriented terminology. This method when called, allows the class to initialize the attributes of the class. The super() function allows us to avoid using the base class name explicitly.

Is super () necessary Python?

In general it is necessary. And it's often necessary for it to be the first call in your init. It first calls the init function of the parent class ( dict ).

How does super () work in Python?

The super() function in Python makes class inheritance more manageable and extensible. The function returns a temporary object that allows reference to a parent class by the keyword super. The super() function has two major use cases: To avoid the usage of the super (parent) class explicitly.

What is __ del __ in Python?

The __del__() method is a known as a destructor method in Python. It is called when all references to the object have been deleted i.e when an object is garbage collected.


1 Answers

The role of __del__ is not to delete the object: it is called before the object is automatically deleted. Therefore it's fine if your parent class doesn't define __del__. Feel free not to call super().__del__() if it's bugging you.

For the record, the reason why objects don't have a default __del__ is that objects with __del__ were not garbage collected in case of reference cycles (until Python 3.4). For more information, read the documentation for gc.garbage in Python 3.3 and for gc.garbage in Python 3.4.

like image 160
Andrea Corbellini Avatar answered Sep 26 '22 14:09

Andrea Corbellini