我不明白为什么我的 super() 会产生错误
i dont understand why my super() is producing an error
如果我这样编码,为什么我的程序可以工作:
class Shape:
class Shape:
shape_type = ""
def __init__(self, c='*'):
self.color = c
class Square(Shape):
shape_type = "Quadrat"
def __init__(self, w, *args, **kwargs):
Shape.__init__(self, *args, **kwargs)
self._width = w
q1 = Square(4, c="#")
但如果我使用 super() 函数则不会
class Shape:
shape_type = ""
def __init__(self, c='*'):
self.color = c
class Square(Shape):
shape_type = "Quadrat"
def __init__(self, w, *args, **kwargs):
super().__init__(self, *args, **kwargs)
self._width = w
q1 = Square(4, c="#")
它说 TypeError: init() got multiple values for argument 'c'.
移除self
:
class Shape:
shape_type = ""
def __init__(self, c='*'):
self.color = c
class Square(Shape):
shape_type = "Quadrat"
def __init__(self, w, *args, **kwargs):
super().__init__(*args, **kwargs)
self._width = w
q1 = Square(4, c="#")
使用 super()
时,您不必提供 self
参数,因为您是在实例上执行 __init__
,而不是 class .
如果我这样编码,为什么我的程序可以工作:
class Shape:
class Shape:
shape_type = ""
def __init__(self, c='*'):
self.color = c
class Square(Shape):
shape_type = "Quadrat"
def __init__(self, w, *args, **kwargs):
Shape.__init__(self, *args, **kwargs)
self._width = w
q1 = Square(4, c="#")
但如果我使用 super() 函数则不会
class Shape:
shape_type = ""
def __init__(self, c='*'):
self.color = c
class Square(Shape):
shape_type = "Quadrat"
def __init__(self, w, *args, **kwargs):
super().__init__(self, *args, **kwargs)
self._width = w
q1 = Square(4, c="#")
它说 TypeError: init() got multiple values for argument 'c'.
移除self
:
class Shape:
shape_type = ""
def __init__(self, c='*'):
self.color = c
class Square(Shape):
shape_type = "Quadrat"
def __init__(self, w, *args, **kwargs):
super().__init__(*args, **kwargs)
self._width = w
q1 = Square(4, c="#")
使用 super()
时,您不必提供 self
参数,因为您是在实例上执行 __init__
,而不是 class .