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've created this class that basically abstracts a row, realize that I made use of setters decorators in a way that does not allow value entry less than 0.
But when I try to create a Line instance if I pass one of the parameters as less than 0 it does so by creating 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 even worse if I pass the two negative attributes the object will be created but it will not have any attributes.
My question is: how do I avoid creating an invalid object? In other words, if one or more invalid parameters are passed, the object is not created.