1
class Linha(object):
def __init__(self, x, y):
self.x = x
self.y = y
@property
def x(self):
return self._x
@x.setter
def x(self, valor):
if valor >= 0:
self._x = valor
@property
def y(self):
return self._y
@y.setter
def y(self, valor):
if valor >= 0:
self._y = valor
I created this class that basically abstracted a line, realize that I made use of decorators setters so that do not allow entry of value less than 0.
But when I try to create an instance of Line if I pass one of the parameters as less than 0 it still makes the creation of the object but does not create this attribute.
Example:
linha = Linha(-1, 1)
If I do this it will create the object but it will not have the attribute x because it will not be set. If worse still if I pass the two negative attributes the object will be created but will not have any attribute.
My question is this: how do I avoid creating an invalid object? That is, if one or more invalid parameters are passed, the object is not created.
I believe that this is the case to make an exception, instead of transparently ignoring the attribution of the attribute. By the way, you’ve abstracted a point in the first quadrant, not exactly a line. You can even generate a line from this information, but in this case you would be being misleading.
– Jefferson Quesado