Is it possible to create a class that inherits from multiple instances of namedtuple
, or create something to the same effect (having an immutable type that combines the fields of the base types)? I haven't found a way to do so.
This example illustrates the problem:
>>> class Test(namedtuple('One', 'foo'), namedtuple('Two', 'bar')):
>>> pass
>>> t = Test(1, 2)
TypeError: __new__() takes 2 positional arguments but 3 were given
>>> t = Test(1)
>>> t.foo
1
>>> t.bar
1
The problem seems to be that namedtuple
does not use super
to initialize its base class, as can be seen when creating one:
>>> namedtuple('Test', ('field'), verbose=True)
[...]
class Test(tuple):
[...]
def __new__(_cls, field,):
'Create new instance of Test(field,)'
return _tuple.__new__(_cls, (field,))
Even if I considered writing my own version of namedtuple
to fix this, it is not obvious how to do that. If there are multiple instances of namedtuple
in the MRO of a class they'd have to share a single instance of the base class tuple
. To do that, they'd have to coordinate on which namedtuple
uses which range of indices in the base tuple.
Is there any simpler way to achieve multiple inheritance with a namedtuple
or something similar? Has anyone already implemented that somewhere?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…