0
I’m trying to define the type of information that attributes receive, but I’m not sure how to do it. I tried these methods, but they do nothing.
class Lancamento:
def __init__(self, tipo, data, descricao, valor, conta,
categoria, tag, repeticao, pago):
self.tipo = tipo
self.data = data
self.descricao = descricao
self._valor = valor
self.conta = conta
self.categoria = categoria
self.tag = tag
self.repeticao = repeticao
self.pago = pago
@property
def valor(self):
return self._valor
@valor.setter
def valor(self):
if type(self._valor) != float:
raise ValueError("...")
# @valor.setter
# def valor(self, value):
# if not isinstance(value, float):
# print("Precisa ser float")
The part in comment was another method, which also did not return the desired, because the object can still be built with any type.
Why in
__init__
you didself._valor = valor
instead ofself.valor = valor
? The second way you will be using the property you defined.– Woss
@Woss If I put self.value = value I have a return of self.value = value Typeerror: value() takes 1 positional argument but 2 Were Given
– William Gabriel Lima
True, in your
setter
a parameter was missing. Osetter
of a property always takes two parameters: theself
and the new value of the property that has been assigned, as it did in the commented code.– Woss
I was able to solve the problem by constructing the class <code> if value < 0: raise Valueerror("Can’t be negative") Elif not isinstance(value, float): raise Valueerror("Needs to be float") Else: self. _value = value </code> @Woss
– William Gabriel Lima
It wasn’t simpler to fix the property?
– Woss
I started programming in python recently, and the material I found to solve this problem did not generate the expected result, only this solution. Not even the direction proposed to me here (and which I marked as the answer) was effective in solving the problem.
– William Gabriel Lima