-
Notifications
You must be signed in to change notification settings - Fork 557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overriding the Polygon class fails due to __new__() #2002
Comments
Update: you can always work around the issue by adding
to MyClass |
I'm designing a Python square class that inherits properties from Shapely's Polygon. Upon initialization, it only requires input of the center point and side length. Here is my code and the error I met, how to deal with it? class SquareClass:
def __init__(self, center, side_length):
self.center = Point(center)
self.side_length = side_length
# Calculate the coordinates of the square's vertices
half_side = side_length / 2
x, y = center
vertices = [(x - half_side, y - half_side),
(x + half_side, y - half_side),
(x + half_side, y + half_side),
(x - half_side, y + half_side)]
super().__new__(vertices)
def __new__(cls, shell=None, holes=None, *args, **kwargs):
return Polygon.__new__(cls, shell, holes)
center = (0, 0)
side_length = 1
square = SquareClass(center, side_length)
print("Square area:", square.area) ---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[16] center = (0, 0)
[17] side_length = 1
---> [18] SquareClass(center, side_length)
[19] print("Square area:", square.area)
[13] def __new__(cls, shell=None, holes=None, *args, **kwargs):
---> [14] return Polygon.__new__(cls, shell, holes)
File Scripts\StationGA\.venv\lib\site-packages\shapely\geometry\polygon.py:230, in Polygon.__new__(self, shell, holes)
[228] return shell
[229] else:
--> [230] shell = LinearRing(shell)
[232] if holes is not None:
[233] if len(holes) == 0:
[234] # shapely constructor cannot handle holes=[]
File Scripts\StationGA\.venv\lib\site-packages\shapely\geometry\polygon.py:93, in LinearRing.__new__(self, coordinates)
[90] else:
[91] return [float(c) for c in o]
---> [93] coordinates = np.array([_coords(o) for o in coordinates])
[94] if not np.issubdtype(coordinates.dtype, np.number):
...
[89] return o.coords[0]
[90] else:
---> [91] return [float(c) for c in o]
TypeError: 'int' object is not iterable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Polygon class builder is defined as:
def __new__(self, shell=None, holes=None):
When I declare a new class inheriting from it, with a few more arguments, an exception occurs:
TypeError: Polygon.__new__() takes from 1 to 3 positional arguments but 4 were given
For instance, my code could be as simple as:
I think the polygon builder should begin by:
def __new__(cls, shell=None, holes=None, *args, **kwargs):
but I'm no expert
(shapely v 2.0.1)
The text was updated successfully, but these errors were encountered: