-1

I have question for the below code, I could understand that the code has def __init__ function, it is necessary to initialize the Frame class, but I still could not understand that why we need to have wx.Frame.__init__ ? Is it necessary to initialize the wx.Frame object?

import wx

class Frame(wx.Frame):
     def __init__(self, title):
         wx.Frame.__init__(self, None, title=title, size=(350,200))
 
app = wx.App(redirect=True)
top = Frame("Hello World")
top.Show()
app.MainLoop()
khelwood
  • 55,782
  • 14
  • 81
  • 108
Steve
  • 1
  • 1

4 Answers4

0

Is it necessary to initialize the wx.Frame object?

Exactly. The wx.Frame.__init__() is a constructor for a base class. Class Frame inherits from wx.Frame and constructor of Frame automatically calls the constructor of wx.frame.

Have a look at this:

class Foo:
  def __init__(self):
    print('Foo was created!')

class Bar(Foo):
  def __init__(self):
    super().__init__() # It's the same as Foo.__init__
    print('Bar was created!')

B = Bar()
# Output:
# Foo was created!
# Bar was created!

The super() returns a proxy object that delegates method calls to a parent class.

For more see: Built-in Functions - super

777moneymaker
  • 697
  • 4
  • 15
0

In the given code, you are creating object of Frame Class. that takes attributes which are defined in the Frame which is defined in wx module.

When you use wx.Frame.__init__ it loads wx module, go to Frame class of that module and run its init function, so all the attributes that are in init of module Frame class get stored on the class which is on your workspace.

Syed Fasih
  • 23
  • 6
0

The local Frame class inherits all the properties of wx.Frame. I don't believe it's necessary to write __init__ but in this case:

top = Frame("Hello World")

is similar to something like

top = wx.Frame(None, title="Hello World", size=(350,200))

Calling wx.Frame.__init__() isn't necessary but it helps setup the default size for the frame and some other parameters behind the scenes in your new Frame class.

jomccr
  • 1
0

When you are creating the init fucntion in the Frame class, its overriding the inherited class init function.

If you want to extend the functionality of inherited class, you need to initialize that class objects also.

import wx
class Frame(wx.Frame):
    pass

this will be same as above class, as init is not defined in subclass, inherited class init will be used.

ASUR
  • 31
  • 1
  • 6