[Tutor] Multiple inheritance

Cameron Simpson cs at cskk.id.au
Fri May 14 03:04:57 EDT 2021


This seems to have a bunch of "blank" characters which are not Python 
indentation characters (space, TAB). Like, 0xa0 and embedded DEL 
characters. Is this cut/paste from your actual code?

Pretending that the whitespace issues were not there, random other 
comments:

On 14May2021 14:53, Phil <phillor9 at gmail.com> wrote:
>This question relates to multiple inheritance rather than a question 
>about wxPython. The code actually works, or did until I tried to create 
>a separate meter class . The problem, as I see it, is that the MyForm 
>class is not inheriting the Meter class and there are bound to be other 
>errors as well. I'm sorry to burden the list with this much code.

But it does! Try printing MyForm.__mro__ to see the method resolution 
order.

>import wx
>import math
>
>class Meter():

You don't need the () in Python 3.

>    def __init__(self, start_x):
>        self.angle = 180  # start with the pointer pointing to the left
>        self.rad = 0
>        self.start_x = start_x  # the starting point of the pointer
>        self.start_y = 200
>        self.length = 50  # the length of the pointer
>        self.inc_amount = 5

Looks fine.

>class MyForm(wx.Frame, Meter):

Also fine.

>    def __init__(self):
>        wx.Frame.__init__(self, None, wx.ID_ANY, "Meter Test", size=(300, 300))
>        # This results in a syntax error
>        Meter.^?^?^?^?__init__(self, start_x)

Well, yeah! There are literal DEL characters in there!

If you clean that up, or just retype it, it should work.

But... Where does start_x come from? I don't see it in the arguments to 
MyForm.__init__.

>        # I also tried super

super().method is good to finding method in a superclass. Here you need 
to call each superclass __init__ separately with distinct arguments.

>        self.InitUI()
>
>    def InitUI(self):
>
>        self.Bind(wx.EVT_PAINT, self.OnPaint)
>
>        panel = wx.Panel(self, wx.ID_ANY)
>
>        self.timer = wx.Timer(self)
>        self.Bind(wx.EVT_TIMER, self.update, self.timer)
>        self.timer.Start(100)
>
>    def update(self, event):
>        m = Meter(200) # m.self.angle etc is not correct here. 
>Shouldn't the Meter class variables
>                                    #  be inherited and be useable with 
>the need for m.self?

Yes. Once you fix your Meter.__init__(self,...) above it should be fine.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list