调用父级 __init__()

Jam*_*Lam 5 python class super

我正在使用Python 2.6.6。

我已将错误代码缩小到以下两类:

class Graph(object):
  def __init__(self, name):
    self.name = name
    self.testme = 3
Run Code Online (Sandbox Code Playgroud)

class StepPlot(Graph):
  def __init__(self, name):
    print("fdasfdsf")
    print(dir(super(Graph, self)))
    super(Graph, self).__init__(name)
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我实例化StepPlotwith时StepPlot('fdsfa'),出现错误

TypeError: object.__init__() takes no parameters

难道它不能接受一个参数吗?

看着

什么时候调用Python的super().__init__()?

这个班级组织应该有效。

我从根本上错过了一些东西吗?任何帮助,将不胜感激。

小智 4

第一个参数super应该是调用它的类:

super(StepPlot, self).__init__(name)
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅此处的文档链接。