我在Python中生成一系列简单命令,并且这些命令具有各种名称的bool,float和int字段 .

我将它们表示为NamedTuple类,例如move命令

class G1(NamedTuple):
    """
    Move command with speed in mm/s
    """
    x: float = None
    y: float = None
    z: float = None
    speed: float = None
    absolute: bool = True

他们're all specific examples of Gcodes, which is the name of the command language, so I'喜欢有一个名为Gcode的类型 issubclass(G1, Gcode) == True . 然后我可以键入提示各种函数 List[Gcode] 并描述一些应该对所有Gcode都通用的方法 .

我试过定义

class Gcode(NamedTuple):
    pass
class G1(Gcode):
    x: float = None
    ...

但G1的构造函数不再按预期工作 .

很抱歉,如果这是重复的,我在搜索上没有任何运气,因为有很多关于子类化NamedTuples的问题,以便为它们添加字段,这不是我想要做的 .