This question already has an answer here:
这个问题在这里已有答案:
- Python super() raises TypeError 4 answers
Python super()引发TypeError 4答案
I am trying to call the __init__()
method in a superclass, where said method takes arguments, but it doesn't seem to be working. Please see the code below:
我试图在超类中调用__init __()方法,其中所述方法接受参数,但它似乎不起作用。请参阅以下代码:
>>> class A:
def __init__(self, param1, param2):
self._var1 = param1
self._var2 = param2
>>> class B(A):
def __init__(self, param1, param2, param3):
super(B, self).__init__(param1, param2)
self._var3 = param3
>>> a = A("Hi", "Bob")
>>> a._var1
'Hi'
>>> a._var2
'Bob'
>>>
>>> b = B("Hello", "There", "Bob")
Traceback (most recent call last):
File "<pyshell#74>", line 1, in <module>
b = B("Hello", "There", "Bob")
File "<pyshell#69>", line 3, in __init__
super(B, self).__init__(param1, param2)
TypeError: must be type, not classobj
>>>
I have never been able to get this to work. What am I doing wrong? I would ideally like to use super()
over A.__init__(self, <parameters>)
, if this is a possibility (which it must be).
我从来没有能够让这个工作。我究竟做错了什么?理想情况下,我喜欢使用super()而不是A .__ init __(self,
3 个解决方案
#1
As a rule of thumb: in Python 2 your base class should always inherit from object
, as otherwise you're using old style classes with surprising behaviors like the one you describe.
根据经验:在Python 2中,您的基类应始终从object继承,否则您将使用具有令人惊讶的行为的旧样式类,如您所描述的行为。
So try
class A(object):
...
like this:
In [1]: class A(object):
...: def __init__(self, param1, param2):
...: self._var1 = param1
...: self._var2 = param2
...:
In [2]: class B(A):
...: def __init__(self, param1, param2, param3):
...: super(B, self).__init__(param1, param2)
...: self._var3 = param3
...:
In [3]: a = A("Hi", "Bob")
In [4]: a._var1
Out[4]: 'Hi'
In [5]: a._var2
Out[5]: 'Bob'
In [6]: b = B("Hello", "There", "Bob")
In [7]: b._var3
Out[7]: 'Bob'
If you really know what you're doing (at least see the docs and this) and you want to use old-style classes you can't use super()
, but instead need to manually call the super-class's __init__
from the sub class like this:
如果你真的知道你在做什么(至少看到文档和这个)并且你想使用旧式类你不能使用super(),而是需要从sub手动调用超类的__init__像这样的课:
class A:
...
class B(A):
def __init__(self, p1, p2, p3):
A.__init__(p1, p2)
...
#2
You can still use old style classes but in this case base class __init__
method should be called explicitly:
您仍然可以使用旧样式类,但在这种情况下,应该显式调用基类__init__方法:
class A:
def __init__(self, param1, param2):
self._var1 = param1
self._var2 = param2
class B(A):
def __init__(self, param1, param2, param3):
A.__init__(self, param1, param2)
self._var3 = param3
Test:
>>> b = B("Hello", "There", "Bob")
>>> b._var1
'Hello'
>>> b._var2
'There'
>>> b._var3
'Bob'
>>>
#3
The fix is to reference object class when declaring A
修复是在声明A时引用对象类
class A(object):
def __init__(self, param1, param2):
self._var1 = param1
self._var2 = param2
This allows the script to execute successfully.
这允许脚本成功执行。
#1
As a rule of thumb: in Python 2 your base class should always inherit from object
, as otherwise you're using old style classes with surprising behaviors like the one you describe.
根据经验:在Python 2中,您的基类应始终从object继承,否则您将使用具有令人惊讶的行为的旧样式类,如您所描述的行为。
So try
class A(object):
...
like this:
In [1]: class A(object):
...: def __init__(self, param1, param2):
...: self._var1 = param1
...: self._var2 = param2
...:
In [2]: class B(A):
...: def __init__(self, param1, param2, param3):
...: super(B, self).__init__(param1, param2)
...: self._var3 = param3
...:
In [3]: a = A("Hi", "Bob")
In [4]: a._var1
Out[4]: 'Hi'
In [5]: a._var2
Out[5]: 'Bob'
In [6]: b = B("Hello", "There", "Bob")
In [7]: b._var3
Out[7]: 'Bob'
If you really know what you're doing (at least see the docs and this) and you want to use old-style classes you can't use super()
, but instead need to manually call the super-class's __init__
from the sub class like this:
如果你真的知道你在做什么(至少看到文档和这个)并且你想使用旧式类你不能使用super(),而是需要从sub手动调用超类的__init__像这样的课:
class A:
...
class B(A):
def __init__(self, p1, p2, p3):
A.__init__(p1, p2)
...
#2
You can still use old style classes but in this case base class __init__
method should be called explicitly:
您仍然可以使用旧样式类,但在这种情况下,应该显式调用基类__init__方法:
class A:
def __init__(self, param1, param2):
self._var1 = param1
self._var2 = param2
class B(A):
def __init__(self, param1, param2, param3):
A.__init__(self, param1, param2)
self._var3 = param3
Test:
>>> b = B("Hello", "There", "Bob")
>>> b._var1
'Hello'
>>> b._var2
'There'
>>> b._var3
'Bob'
>>>
#3
The fix is to reference object class when declaring A
修复是在声明A时引用对象类
class A(object):
def __init__(self, param1, param2):
self._var1 = param1
self._var2 = param2
This allows the script to execute successfully.
这允许脚本成功执行。