0
class A(object): 
    def routine(self):
        print "A.routine()"
class B(A):
    def routine(self):
        print "B.routine()"
        A().routine()
def fun():
    b = B()
    b.routine()
if __name__ == '__main__':fun()

When I use the above code the A().routine executes the command in method of class A but when I use the code:

import wx

class Example(wx.Frame):

def __init__(self, parent, title):

    wx.Frame().__init__(parent, title=title, size=(300, 200))
    self.Centre()
    self.Show()


if __name__ == '__main__':

    app = wx.App()
    Example(None, title='Size')
    app.MainLoop()

why is it that

wx.Frame().__init__(parent, title=title, size=(300, 200))

does not work similar to

A().routine()

and instead shows the error: TypeError: Required argument 'parent' {pos 1} not found

Anish Shah
  • 163
  • 1
  • 2
  • 13

1 Answers1

0

The code

A().routine()

creates a new A object and calls the method on that object.

To call the base class method for your own object, use this:

super(B, self).routine()

For your example frame, use:

class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title, size=(300, 200))
        ...

If you really do not want to use super, call the base class method explicitly:

class Example(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(300, 200))
        ...

See also Understanding Python super() with __init__() methods.

Community
  • 1
  • 1
CL.
  • 173,858
  • 17
  • 217
  • 259
  • Is it possible to do the same without using the super command? Using super I was getting the result even earlier. What I am looking for here is an alternative by directly using wx.Frame() method – Anish Shah May 27 '13 at 09:26
  • See the link at the bottom. – CL. May 27 '13 at 10:47
  • Again the question comes back to level 0. As my original question says I do not want to use the super function. Is there any alternative by directly using wx.Frame() method? If yes could you please post the solution code to my problem? – Anish Shah May 28 '13 at 03:56
  • No, there is no solution that uses the `wx.Frame()` method. You must use the `wx.Frame` class instead. – CL. May 28 '13 at 06:56