In my Python (3.6) program, I have a thread object, like so:
class MyThread(threading.Thread):
def __init__(self):
super(MyThread, self).__init__()
...
def __del__(self):
...
super(type(self), self).__del__()
def run(self):
...
used in the main program like this:
def main():
my_thread = MyThread()
my_thread.start()
...
my_thread.join()
But as soon as I try to run this program, I get the following Python crash:
Exception ignored in: <bound method MyThread.__del__ of <MyThread(Thread-6, stopped 1234)>>
Traceback (most recent call last):
File "c:/my_proj/my_program.py", line 123, in __del__
super(type(self), self).__del__()
AttributeError: 'super' object has no attribute '__del__'
Why is this, and how can it be fixed?
Is it not allowed to call the __del__()
method of super explicitly like this, or what? (Google seems to tell me otherwise, but still won't give me any answer to why this happens)
super(type(self), self)
is always wrong . In Python 2, you must explicitly name the current class, eg super(MyThread, self)
. In Python 3, you can simply use super()
:
class MyThread(threading.Thread):
def __init__(self):
super().__init__()
# ...
def run(self):
# ...
That said, if if the superclass has no __del__
then you'll get this AttributeError
. If your base classes have no __del__
you can simply omit it. There is rarely a good reason to implement __del__
in your class.
If you need controlled cleanup, consider using implementing a context manager .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.