你好,我有这个代码:
class A:
def __init__(self, name):
self.name = name
class B(A):
def __init__(self):
super.__init__()
但是我没有实现使用B类:
我试图写:
C = B()
但是我得到了:
TypeError
Traceback (most recent call last)
<ipython-input-7-ea9745abfeb2> in <module>()
----> 1 C = B()
<ipython-input-4-2d2ef2cc5406> in __init__(self)
5 class B(A):
6 def __init__(self):
----> 7 super.__init__()
TypeError: descriptor '__init__' of 'super' object needs an argument
能帮我吗?
答案 0 :(得分:3)
您必须为super提供参数,
class A:
def __init__(self, name):
self.name = name
class B(A):
def __init__(self, name):
super().__init__(name)
或name
中A
的默认参数
class A:
def __init__(self, name=''):
self.name = name
class B(A):
def __init__(self):
super().__init__() # this works, but is probably not a good idea
# as you might need to pass a name to A, at some moment
也许在您的情况下,B
不需要__init__
?
class A:
def __init__(self, name=''):
self.name = name
class B(A):
pass
答案 1 :(得分:0)
这些对我有用
对于python 3:
class A:
def __init__(self, name):
self.name = name
class B(A):
def __init__(self):
super().__init__('temp name')
C = B()
对于python 2:
class A(object):
def __init__(self, name):
self.name = name
class B(A):
def __init__(self):
super(B, self).__init__("any name")
C = B()