как сделать перегрузку конструктора класса python
Мне нужно сделать перегрузку именно без args.
Можно делать через isinstance, но мне хочется чтобы все эти конструкторы были видны в редакторе кода. Такое как раз предоставляет overload из typing.
from typing import Tuple, overload
from pygame import Vector2
class Vector2i:
@overload
def __init__(self, x: int = 0, y: int = 0):
self.x = x
self.y = y
@overload
def __init__(self, xy: Tuple[int, int]):
self.x = xy[0]
self.y = xy[1]
@overload
def __init__(self, xy: Tuple[float, float]):
self.x = int(xy[0])
self.y = int(xy[1])
@overload
def __init__(self, xy: Vector2):
self.x = int(xy.x)
self.y = int(xy.y)
def to_vector2(self) -> Vector2:
return Vector2(float(self.x), float(self.y))
position = Vector2i(60, 60) # NotImplementedError: You should not call an overloaded function. A series of @overload-decorated functions outside a stub module should always be followed by an implementation that is not @overload-ed.
print(position.x, position.y)