我在网上读到,重载构造函数的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]
上面的代码是一个示例,目前运行良好。但为了可读性和可维护性;作为一种最佳实践,使用最少的参数初始化对象是非常有用的,尤其是当对象随着时间的推移变得更加复杂时
在调用
__init__
时,对象已经被构造,所以现在使用from_tuples
已经太晚了不要用参数的数量来衡量简单性。相反,考虑哪些方法可以用来实现其他方法。如果希望元组成为矩形的基本构造块,可以这样做:
相关问题 更多 >
编程相关推荐