I have a base class Polygon which holds a collection of vertices. It can also be constructed using the classmethod from_number_of_vertices_and_edge_length, in which case an equilateral polygon is constructed.
I also have a derived class, Square. I'm trying to initialize Square using the classmethod of Polygon.
When I try to create a Square instance, it will go into Polygon.from_number_of_vertices_and_edge_length() with cls being Square instead of Polygon, causing the line cls(vertices) to fail because it's calling Square(vertices) whereas I require it to call Polygon(vertices).
How can I solve this?
class Polygon:
def __init__(self, vertices: list[Point]):
self.vertices = vertices
@classmethod
def from_number_of_vertices_and_edge_length(cls, num_vertices, edge_length):
# Create equilateral polygon with 'n' vertices.
vertices = ...
return cls(vertices)
class Square(Polygon):
def __init__(self, edge_length):
self.edge_length = edge_length
super().from_number_of_vertices_and_edge_length(4, edge_length)
EDIT 1:
I'd like the square instance to end up with attributes edge_length and vertices.