Python 为什么它有双初始化函数?

Python 为什么它有双初始化函数?,python,wxpython,Python,Wxpython,我对下面的代码有疑问,我可以理解代码有def\uuuu init\uuuu函数,有必要初始化Frame类,但我仍然无法理解为什么我们需要wx.Frame.\uu init\uuuu?是否需要初始化wx.Frame对象 import wx class Frame(wx.Frame): def __init__(self, title): wx.Frame.__init__(self, None, title=title, size=(350,200)) app =

我对下面的代码有疑问,我可以理解代码有
def\uuuu init\uuuu
函数,有必要初始化Frame类,但我仍然无法理解为什么我们需要
wx.Frame.\uu init\uuuu
?是否需要初始化wx.Frame对象

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()
是否需要初始化wx.Frame对象

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()
准确地说
wx.Frame.\uuuu init\uuuu()
是基类的构造函数。类
Frame
继承自
wx.Frame
,而
Frame
的构造函数会自动调用
wx.Frame
的构造函数

看看这个:

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!
super()
返回将方法调用委托给父类的代理对象。


有关详细信息,请参见:

在给定的代码中,您正在创建
Frame
类的对象。它获取在wx模块中定义的
框架中定义的属性


使用
wx.Frame.\uuuu init\uuuuu
时,它加载wx模块,转到该模块的Frame类并运行其init函数,以便模块Frame类init中的所有属性都存储在工作区中的类上。

本地
Frame
类继承wx.Frame的所有属性。我认为没有必要编写
\uuuu init\uuuu
,但在这种情况下:

top = Frame("Hello World")
类似于

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

调用
wx.Frame.\uuu init\uuu()
不是必需的,但它有助于在新的
Frame
类中设置帧的默认大小和一些其他幕后参数。

在Frame类中创建init函数时,它将覆盖继承的类init函数

如果要扩展继承类的功能,还需要初始化该类对象

import wx
class Frame(wx.Frame):
    pass
这将与上面的类相同,因为子类中未定义init,将使用继承类init

是否回答了您的问题?