如何在多重继承中使用namedtuples

Bjö*_*lex 1 python inheritance multiple-inheritance namedtuple

是否可以创建一个继承自多个实例的类namedtuple,或者创建具有相同效果的类(具有组合基类型字段的不可变类型)?我还没有找到办法.

这个例子说明了这个问题:

>>> 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
Run Code Online (Sandbox Code Playgroud)

问题似乎是namedtuplesuper用于初始化其基类,如创建一个时可以看到:

>>> namedtuple('Test', ('field'), verbose=True)
[...]    
class Test(tuple):
[...]
    def __new__(_cls, field,):
        'Create new instance of Test(field,)'
        return _tuple.__new__(_cls, (field,))
Run Code Online (Sandbox Code Playgroud)

即使我考虑编写自己的版本namedtuple来解决这个问题,但如何做到这一点并不明显.如果namedtuple类的MRO中有多个实例,则它们必须共享基类的单个实例tuple.要做到这一点,他们必须协调namedtuple使用基本元组中哪个索引范围.

有没有更简单的方法来实现与namedtuple类似或类似的多重继承?有人已经在某处实现了吗?

Din*_*off 5

您可以使用装饰器或元类将父命名元组字段组合成新的命名元组并将其添加到类中__bases__:

from collections import namedtuple

def merge_fields(cls):
    name = cls.__name__
    bases = cls.__bases__

    fields = []
    for c in bases:
        if not hasattr(c, '_fields'):
            continue
        fields.extend(f for f in c._fields if f not in fields)

    if len(fields) == 0:
        return cls

    combined_tuple = namedtuple('%sCombinedNamedTuple' % name, fields)
    return type(name, (combined_tuple,) + bases, dict(cls.__dict__))


class SomeParent(namedtuple('Two', 'bar')):

    def some_parent_meth(self):
        return 'method from SomeParent'


class SomeOtherParent(object):

    def __init__(self, *args, **kw):
        print 'called from SomeOtherParent.__init__ with', args, kw

    def some_other_parent_meth(self):
        return 'method from SomeOtherParent'


@merge_fields
class Test(namedtuple('One', 'foo'), SomeParent, SomeOtherParent):

    def some_method(self):
        return 'do something with %s' % (self,)


print Test.__bases__
# (
#   <class '__main__.TestCombinedNamedTuple'>, <class '__main__.One'>, 
#   <class '__main__.SomeParent'>, <class '__main__.SomeOtherParent'>
# )
t = Test(1, 2)  # called from SomeOtherParent.__init__ with (1, 2) {} 
print t  # Test(foo=1, bar=2)
print t.some_method()  # do something with Test(foo=1, bar=2)
print t.some_parent_meth()  # method from SomeParent
print t.some_other_parent_meth()  # method from SomeOtherParent
Run Code Online (Sandbox Code Playgroud)


PM *_*ing 5

该代码采用了与 Francis Colas 类似的方法,尽管它有点长:)

它是一个工厂函数,它接受任意数量的父命名元组,并创建一个新的命名元组,其中按顺序包含父级中的所有字段,并跳过任何重复的字段名称。

from collections import namedtuple

def combined_namedtuple(typename, *parents):
    #Gather fields, in order, from parents, skipping dupes
    fields = []
    for t in parents:
        for f in t._fields:
            if f not in fields:
                fields.append(f)
    return namedtuple(typename, fields)

nt1 = namedtuple('One', ['foo', 'qux'])
nt2 = namedtuple('Two', ['bar', 'baz'])    

Combo = combined_namedtuple('Combo', nt1, nt2)    
ct = Combo(1, 2, 3, 4)
print ct
Run Code Online (Sandbox Code Playgroud)

输出

Combo(foo=1, qux=2, bar=3, baz=4)
Run Code Online (Sandbox Code Playgroud)