从“list”继承的类中正确的初始化定义

2024-09-30 16:32:21 发布

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

我试图为从list继承的类定义一个init函数的“好方法”。下面是非常基本的代码(我只保留了必要的代码):

class measurementPoint(list) :
    """measurementPoint class : contains all the pairedMeasurement at a given temperature for a given channel"""

    def __init__(self, item):
        try:
            assert isinstance(item,pairedMeasurement)
        except AssertionError:
            print("Wrong type for object " + str(item))
            sys.exit(1)
        super().__init__(self)
        self.append(item)

初始化这样一个对象,难道没有比super().__init__后跟append更好的方法吗?我想应该有一个,但不知道怎么做


Tags: 方法函数代码selffor定义inititem