Orientation to Python Objects

Asked

Viewed 71 times

0

How to make the only way to insert the days, months and years are solely and exclusively done by the setDia() method, that is, when instantiating do not want to pass values to these attributes at that time.

Follows code below:

class Data():

def __init__(self, dia = 1, mes = 1, ano = 1980):
    self._dia = dia
    self._mes = mes
    self._ano = ano
    if not self._valida(): #Depois que os dados sao passados para os atributos, chamamos a funcao de validacao para validar os dados
        raise ValueError('Erro! Data invalida.')

def __str__(self):
    return f'{self.getDia()}/{self.getMes()}/{self.getAno()}'

def _valida(self):
    if not 1 <= self._dia <= 31:
        return False
    if not 1 <= self._mes <= 12:
        return False
    if self._ano < 1:
        return False
    return True

def eAnterior(self, dataComp):
    if dataComp.getAno() > self.getAno():
        return False
    elif dataComp.getAno() == self.getAno() and dataComp.getMes() > self.getMes():
        return False
    elif dataComp.getAno() == self.getAno() and dataComp.getMes() == self.getMes() and dataComp.getDia() >= self.getDia():
        return False
    else:
        return True

def ePosterior(self, dataComp):
    if dataComp.getAno() < self.getAno():
        return False
    elif dataComp.getAno() == self.getAno() and dataComp.getMes() < self.getMes():
        return False
    elif dataComp.getAno() == self.getAno() and dataComp.getMes() == self.getMes() and dataComp.getDia() <= self.getDia():
        return False
    else:
        return True

def eSimultaneo(self, dataComp):
    if dataComp.getAno() != self.getAno():
        return False
    elif dataComp.getAno() == self.getAno() and dataComp.getMes() != self.getMes():
        return False
    elif dataComp.getAno() == self.getAno() and dataComp.getMes() == self.getMes() and dataComp.getDia() != self.getDia():
        return False
    else:
        return True    

def setData(self, dia, mes, ano):
    TODO: 'Preciso fazer com que essa funcao se torne a unica maneira de setar os atributos, sendo todos ou apenas um ou dois.'
    self._dia = dia
    self._mes = mes
    self._ano = ano
    if not self._valida():
        self._dia = 1
        self._mes = 1
        self._ano = 1980
        raise ValueError('Erro! Data invalida.')

@property
def _dia(self):
    return self._dia

@property
def _mes(self):
    return self._mes

@property
def _ano(self):
    return self._mes

@dia.setter
def _dia(self, dia):
    self._dia = dia
    self._valida()

@mes.setter    
def _mes(self, mes):
    self._mes = mes
    self._valida()

@ano.setter
def _ano(self, ano):
    self.ano = ano
    self._valida()  

And that same code, when I try to run it it returns me an error:

Traceback (most recent call last):
File "Data.py", line 1, in <module>
 class Data():
File "Data.py", line 75, in Data
 @dia.setter
NameError: name 'dia' is not defined

But the dia, is set up there on __init__.

  • the error refers to the Setter and not the property, the problem is in the names of the functions Setter and getter, use get_dia for the function getter, @get_dia.Setter for the decorator

1 answer

3


I think you should make the Setter and getter another way, I made a simple example for demo

class GetSet:
    def __init__(self, arg):
        self._valor = arg

    @property
    def valor(self):
        return self._valor

    @valor.setter
    def valor(self, arg):
        self._valor = arg

And the way to make use

teste = GetSet(10)
print(teste.valor)
teste.valor = 15
print(teste.valor)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.