Python subclassing process with initialiser
When you add your own __init__()
here, you are overriding the __init__()
in the superclass. However, the superclass often (as in this case) has some stuff it needs in its __init__()
. Therefore, you either have to re-create that functionality (e.g. initializing _popen
as described in your error, among other things), or call the superclass constructor within your new constructor using super(My_class, self).__init__()
(or super().__init__()
in Python 3).
Well, you don't have a _popen in your class.
If _popen was declared at the class level or was a function in mp.Process, then your code would work because it would grab it off the Process namespace.
class Process(object):
_popen = None
def __init__(...):
#stuff, but not involving _popen
The assert looks like a guard however and I'll guess that the code looks somewhat like:
class Process(object):
def __init__(...):
#let's make sure that we don't open twice
self._popen = None
Now, in this case, _popen is only set on the instance, not on the Process class. But in order for it to be set, you need to execute that mp.Process.__init__ code.
Your code would work perfectly well, if you called 'Process.__init__', which you didn't originally do.
Instead of using Super, you could call it instead with
class My_Class(mp.Process):
def __init__(self):
#
mp.Process.__init__(self)
....do your stuff....
this is what I used to do and it works fine. But it breaks if you change the inheritance to say My_Class(mp.AnotherProcessType). In that case, any calls to mp.Process.somefunc(), not just __init__ would need to be adjusted manually.
super(My_Class, self).init() ends up doing the same exact thing in this case but is a more robust way to do the housework in order to call mp.Process.__init__.
You can sometimes away with not calling an init on a the ancestor of a Python class. But it all depends on whether there is initialization code that needs running. And in this case, it looks like there is.
Also if you didn't have an __init__ in your class then you also wouldn't have to worry, mp.Process.__init__ would have been called automatically. But the existence of your own __init__ basically says "I'll do my own initialization, thank you very much". It's up to your code to explicitly delegate some work back to the ancestor class if necessary.
p.s. not to worry, I find super(xxx,self).somefunc() a bit un-pythonically obscure as well. But it works.