导入输入类 MultipleInheritanceNamedTupleMeta(typing.NamedTupleMeta):def __new__(mcls, typename, bases, ns):如果在基数中键入.NamedTuple:base = super().__new__(mcls, '_base_' + typename, bases, ns)bases = (base, *(b for b in bases if not isinstance(b, typing.NamedTuple)))return super(typing.NamedTupleMeta, mcls).__new__(mcls, typename, bases, ns)类 Base1(元类=MultipleInheritanceNamedTupleMeta):def foo(self):打印(自我.__注释__)类 Test1(NamedTuple,Base1):x: 整数y:整数请注意,这不会让您继承字段!那是因为您必须为任何字段组合生成一个新的 namedtuple 类.以上产生了以下结构:Test1,继承自_base_Test1 - 实际的 typing.NamedTuple 生成的 namedtuple元组Base1这按要求工作:>>>x = 测试 1(1, 2)>>>x.foo(){'x':,'y':}I'm using NamedTuples to hold data, and I want to add a method that can be inherited by multiple NamedTuple based classes. But when I try using multiple inheritance or subclassing NamedTuple based classes, it doesn't work. Specifically, I'm trying to automatically give all of my data classes a method that can look at the classes annotations and then call some serializing code based on that. Here are some examples of what I've tried:from typing import NamedTupleclass Base1: def foo(self): print(self.__annotations__)class Test1(NamedTuple, Base1): x: int y: intx = Test1(1, 2)x.foo() # raises AttributeErrorclass Base2(NamedTuple): def foo(self): print(self.__annotations__)class Test2(Base2): x: int y: intx = Test2(1, 2) # TypeError: __new__() takes 1 positional argument but 3 were givenis there a way for me to use the NamedTuple class like this? 解决方案 At issue is the metaclass used by typing.NamedTuple; this metaclass ignores all base classes and just generates a collections.namedtuple() class with added annotation information (copying across any additional attributes directly defined on the class).You can define your own metaclass (which must be a subclass of typing.NamedTupleMeta), that adds your additional base classes after generating the named tuple class:import typingclass MultipleInheritanceNamedTupleMeta(typing.NamedTupleMeta): def __new__(mcls, typename, bases, ns): if typing.NamedTuple in bases: base = super().__new__(mcls, '_base_' + typename, bases, ns) bases = (base, *(b for b in bases if not isinstance(b, typing.NamedTuple))) return super(typing.NamedTupleMeta, mcls).__new__(mcls, typename, bases, ns)class Base1(metaclass=MultipleInheritanceNamedTupleMeta): def foo(self): print(self.__annotations__)class Test1(NamedTuple, Base1): x: int y: intNote that this won't let you inherit fields! That's because you must generate a new namedtuple class for any combination of fields. The above produces the following structure:Test1, inherits from_base_Test1 - the actual typing.NamedTuple generated namedtupletupleBase1and this works as required:>>> x = Test1(1, 2)>>> x.foo(){'x': <class 'int'>, 'y': <class 'int'>} 这篇关于自定义typing.NamedTuple的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-11 14:27