Runtime error: super-class __init__() never called

As I mentioned in a previous message, I’m trying to get Dabo to work with Python3 and wxPython 4.x. Dabo works mainly by the use of mixin classes that add a consistent interface to all objects, but these mixins can make debugging difficult sometimes.

As I’ve progressed, I’m now seeing the error message in the subject appearing consistently. So I’ve created a small sample program that illustrates the problem using a very simple mixin class:

simple.py

import wx

class SimpleMixin():

def __init__(self, name=None):

    print("Mixin init")

    self._name = name or "Default"

    print("Mixin init DONE")

def simple_method(self):

    print(id(self), self._name)

class SimpleFrame(SimpleMixin, wx.Frame):

def __init__(self, parent, name=None):

    print("Subclass init")

    super(SimpleFrame, self).__init__(name)

    print("Subclass init DONE")

if name == “main”:

app = wx.App()

f = SimpleFrame(None, "Test")

f.Show()

app.MainLoop()

When I run this program, I get the following output:

(wxenv)ed@imac:~/projects/dabo(2phase)$ python simple.py

Subclass init

Mixin init

Mixin init DONE

Subclass init DONE

Traceback (most recent call last):

File “simple.py”, line 22, in

f.Show()

RuntimeError: super-class init() of type SimpleFrame was never called

(wxenv)ed@imac:~/projects/dabo(2phase)$

It shows that the init() methods of both the subclass and the mixin were called and completed in the expected order, so I don’t understand why I’m seeing this error message.

– Ed Leafe

[…]

When I run this program, I get the following output:

(wxenv)ed@imac:~/projects/dabo(2phase)$ python simple.py

Subclass
init

Mixin init

Mixin init DONE

Subclass init DONE

Traceback (most recent call last):

File “simple.py”, line 22, in

f.Show()

RuntimeError:
super-class init() of type SimpleFrame was never called

(wxenv)ed@imac:~/projects/dabo(2phase)$

It shows that the init() methods of both the subclass and the mixin were called and completed in the expected order, so I don’t understand why I’m seeing this error message.

The mixin class also needs to call super(…).init in order for the MRO chain to continue to be followed. See

···

https://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance

Robin

yup – there are three guidelines for using super – and both seminal articles:

“Super Considered Harmful”

https://fuhm.net/super-harmful/

and

“Super considered Super”

https://rhettinger.wordpress.com/2011/05/26/super-considered-super/

say similar things:

  • the method being called by super() needs to exist
  • the caller and callee need to have a matching argument signature
  • and every occurrence of the method needs to use super()
    That last one is killing you – nothing is calling wx.Frame’s init

SimpleFrame’s init is calling super()'s init, which calls SimpleMixin’s init – but then it’s done. SImpleMixin’s init isn’t calling anything else, so wx.Frame’s init never gets called.

The way I think of it is this:

super() does not assure that all the superclasses’ methods get called – rather, what it does is assure that each one in the MRO gets called only once.

The basic process of only one method getting called still holds – the whole stack only gets called if EVERY method in the stack calls its superclass method – whether by super() or directly.

Maybe this will help:

https://uwpce-pythoncert.github.io/PythonCertDevel/modules/MultipleInheritance.html

If not – let me know, I’d like to improve it :slight_smile: You can post issues or PRs on:

https://github.com/UWPCE-PythonCert/PythonCertDevel

-CHB

···

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov