Python命名的耦合工厂方法

2024-09-20 23:03:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一节课

class Instruction(NamedTuple):
    function: str
    argument_register_1: str
    argument_register_2: str
    result_register: str

以及扩展它的类:

class OrderedInstruction(Instruction):
    order: int

    @classmethod
    def from_instruction(cls, instruction: Instruction, order: int):
        return cls(instruction.function,
                   instruction.argument_register_1,
                   instruction.argument_register_2,
                   instruction.result_register,
                   order)

但问题是当我试图调用OrderedInstruction.from_instruction(instruction, 0)时,我得到了异常

TypeError: __new__() takes 5 positional arguments but 6 were given

我不知道为什么。我无法调试它,因为我没有看到对__new__的调用。谁能告诉我如何创建/使用OrderedInstruction.from_instruction(instruction, number)


Tags: fromregisterneworderfunctionresultargumentclass

热门问题