You have several errors, besides the one that causes the syntax error:
-
In the radio_polar
method you need to close a parenthesis, it should be
return math.sqrt((self.x * self.x) + (self.y * self.y))
This is the error you get. Faced with these errors, I recommend looking at the line before the one marked by the error, given that it is common for the cause to be the lack of closure of a parenthesis, key, etc. in that line. It is worth mentioning that if IDLE is used as an IDE this type of errors are not very self-explanatory, just an error popup appears warning that there is a syntax error without more information, but if you notice the start of the line appears (in your case the def
) underlined indicating the line of the error.
-
In dist_euclidiana
the definition is incorrect, it should be:
def dist_euclidiana(self, other):
Override the name of the class before the parameter, this causes another syntax error.
-
In your methods you must prefix self
to the names of the instance attributes as you do correctly in dist_euclidiana
.
-
You are incorrectly calculating the polar angle, it should be math.atan2(y, x)
. math.atan2
receives two values ( y
, x
) and calculates the value of atan(y/x)
. In your code you only pass a value ( x / y
) and also reverse the order of the coordinates.
On the other hand, if with get_x
and get_y
you limit yourself to return the value of the attribute, you should dispense with both methods and simply directly access the attributes of the instance. If you want to validate the parameters (for example, accept only integers), consider using properties, which is the most "pythonic" way to define what are known as setters and getters in other languages.
The code might look something like this:
import math
class Punto2D:
"""Representacion de punto en 2 dimenciones"""
def __init__(self, x, y):
self.x = x
self.y = y
@property
def x(self):
return self._x
@x.setter
def x(self, value):
if not isinstance(value, int):
raise ValueError("x debe ser un entero")
self._x = value
@property
def y(self):
return self._y
@y.setter
def y(self, value):
if not isinstance(value, int):
raise ValueError("y debe ser un entero")
self._y = value
def radio_polar(self):
return math.sqrt((self._x * self._x) + (self._y * self._y))
def angulo_polar(self):
return math.atan2(self._y, self._x)
def dist_euclidiana(self, other):
if not isinstance(other, Punto2D):
raise ValueError("El parámetro <other> debe ser una instancia de Punto2D")
dx = self.x - other.x
dy = self.y - other.y
return math.sqrt((dx * dx) + (dy * dy))
Example of output:
>>> A = Punto2D(2, 6)
>>> B = Punto2D(1, 2)
>>> A.radio_polar()
6.324555320336759
>>> A.angulo_polar()
1.2490457723982544
>>> A.dist_euclidiana(B)
4.123105625617661
>>> A.x
2
>>> A.y
6
>>> B.x
1
>>> B.y
2
Note: In this case, the setter only validates integers, any other instance passed on initialization or when assigning a value to the attributes x
e y
will subsequently cause an exception ( ValueError
). If for example you would also allow float
you only have to do if not isinstance(value, (int, float)):