【发布时间】:2020-02-05 04:32:29
【问题描述】:
我在网上看到重载构造函数的pythonic方法是创建类方法。所以我创建了一个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]
上面的代码是一个示例,现在可以正常工作。但是为了可读性和可维护性;作为最佳实践,使用最少的参数初始化对象会很有用,尤其是当它们随着时间的推移变得更加复杂时。
【问题讨论】:
标签: python inheritance methods initialization superclass