Runtime error: super-class __init__() never called

2,595 views
Skip to first unread message

Ed Leafe

unread,
Jan 26, 2019, 4:18:02 PM1/26/19
to wxPython-users
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 <module>
    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

Robin Dunn

unread,
Jan 26, 2019, 4:44:32 PM1/26/19
to wxpytho...@googlegroups.com
Ed Leafe wrote on 1/26/19 1:18 PM:
[...]

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 <module>
    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

Chris Barker

unread,
Jan 29, 2019, 8:26:46 PM1/29/19
to wxpython-users
yup -- there are three guidelines for using super -- and both seminal articles:

"Super Considered Harmful"

and 

"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:


If not -- let me know, I'd like to improve it :-) You can post issues or PRs on:


-CHB









--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

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....@noaa.gov
Reply all
Reply to author
Forward
0 new messages