如何从超类方法初始化子类?

2024-06-25 06:27:40 发布

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

我在网上读到,重载构造函数的python方法是创建类方法。所以我创建了一个RectF类,可以用两种方法之一初始化它

class RectF:
    def __init__(self, x: float, y: float, w, h):
        self.x: float = x
        self.y: float = y
        self.width = w
        self.height = h

    @classmethod
    def from_tuples(cls, pos: tuple, size: tuple):
        return cls(pos[0], pos[1], size[0], size[1])

init构造函数为每个字段使用一个参数,而from_tuples方法使用两个元组,分别包含坐标和大小

但是,当我使用from_tuples方法初始化子类的实例时,会引发异常。使用super().__init__()效果很好

class Entity(RectF):
    def __init__(self, pos: tuple, size: tuple, vel: tuple):
        super().__init__(pos[0], pos[1], size[0], size[1])

        # I would like to initialize the superclass using the from_tuples class method.
        # super().from_tuples(pos, size)
        # This throws the following exception: __init__() takes 4 positional arguments but 5 were given

        self.vel_x = vel[0]
        self.vel_y = vel[1]

上面的代码是一个示例,目前运行良好。但为了可读性和可维护性;作为一种最佳实践,使用最少的参数初始化对象是非常有用的,尤其是当对象随着时间的推移变得更加复杂时


Tags: the方法fromposselfsizeinitdef
1条回答
网友
1楼 · 发布于 2024-06-25 06:27:40

在调用__init__时,对象已经被构造,所以现在使用from_tuples已经太晚了

不要用参数的数量来衡量简单性。相反,考虑哪些方法可以用来实现其他方法。如果希望元组成为矩形的基本构造块,可以这样做:

class RectF:
    def __init__(self, pos: tuple, size: tuple):
        self.x: float = pos[0]
        self.y: float = pos[1]
        self.width = size[0]
        self.height = size[1]

    # No good name for this method comes to mind
    @classmethod
    def from_separate_values(cls, x, y, w, h):
        return cls((x, y), (w, h))


class Entity(RectF):
    def __init__(self, pos: tuple, size: tuple, vel: tuple):
        super().__init__(pos, size)
        self.vel_x = vel[0]
        self.vel_y = vel[1]

    @classmethod
    def from_separate_values(cls, x, y, w, h, vx, vy):
        rv = super().from_separate_values(x, y, w, h)
        rv.vel_x = vx
        rv.vel_y = vy
        return rv

相关问题 更多 >