I am trying to understand how to subclass a Thread,
and I am confused with some details of inheritance.
It seems if I want to modify
the __init__ I need to call super as follows:
attributes as I am using *args, **kwargs and I can call MyThread._target within the initializer and it works.
However it seems I don't need to call super to
modify the run method. I have seen this example online:
However, the threading.Thread.__init__(self) is not
calling any parameters, so it's essentially an empty __init__ and
doesn't acquire any of the parents attributes such as target, args, group.
So it seems as if they are creating a new init. So why even call threading.Thread.__init__ if you are not going to inherit any
attributes?
And why doesn't run require threading.Thread.run() if they are modifying the run method?
It seems only init is requiring the call to the original init
Now I was trying to access the ._target after super inheritance, but the attribute isn't found in the run method:
Thank you.
and I am confused with some details of inheritance.
It seems if I want to modify
the __init__ I need to call super as follows:
class MyThread(threading.Thread): def __init__(self, url, browser, *args, **kwargs): super(MyThread, self).__init__(*args, **kwargs) self.url = url self.browser = browserThis allows me to inherit all of the parents init
attributes as I am using *args, **kwargs and I can call MyThread._target within the initializer and it works.
However it seems I don't need to call super to
modify the run method. I have seen this example online:
class MyThread(threading.Thread): def __init__(self, number, logger): threading.Thread.__init__(self) self.number = number self.logger = logger def run(self): """ Run the thread """ #modification to run method but no call to it logger.debug('Calling doubler') doubler(self.number, self.logger)Here it seems they are overwriting the parent init with The threading.Thread.__init__(self) ?
However, the threading.Thread.__init__(self) is not
calling any parameters, so it's essentially an empty __init__ and
doesn't acquire any of the parents attributes such as target, args, group.
So it seems as if they are creating a new init. So why even call threading.Thread.__init__ if you are not going to inherit any
attributes?
And why doesn't run require threading.Thread.run() if they are modifying the run method?
It seems only init is requiring the call to the original init
Now I was trying to access the ._target after super inheritance, but the attribute isn't found in the run method:
class MyThread(threading.Thread): def __init__(self, number, style, *args, **kwargs): super().__init__(*args, **kwargs) self.number = number self.style = style print(self._target) # works def run(self, *args, **kwargs): super().run(*args, **kwargs) print(self._target) # leads to error print('thread has ended')
custom = MyThread(target = print, number = 3, style ="red", args = ("test",)) custom.run()
Error:<built-in function print>
test
Traceback:
custom.run()
print(self._target)
AttributeError: 'MyThread' object has no attribute '_target'[/python]
So these are the things I am confused about and am hoping someone can clarify these details.Thank you.