这个问题要求的是在Python中从基类继承一个命名元组的相反情况,也就是从一个命名元组派生一个子类而不是反过来。
在普通的继承中这样做是可以的:
class Y(object):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
class Z(Y):
def __init__(self, a, b, c, d):
super(Z, self).__init__(a, b, c)
self.d = d
>>> Z(1,2,3,4)
<__main__.Z object at 0x10fcad950>
但如果基类是一个namedtuple
:
from collections import namedtuple
X = namedtuple('X', 'a b c')
class Z(X):
def __init__(self, a, b, c, d):
super(Z, self).__init__(a, b, c)
self.d = d
>>> Z(1,2,3,4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __new__() takes exactly 4 arguments (5 given)
问题是,Python中是否可以将命名元组作为基类进行继承?如果可以,怎么做?