Instantiate a class with private attributes

Asked

Viewed 67 times

2

How can I take the attributes of the person class and instantiate them in the medical class and then display it? The doctor and the patient are people and both have class attributes Pessoa. I left some attributes as private because they are sensitive data.

class Pessoa:
    def __init__(self, nome, rg, cpf, telefone):
        self.nome = nome
        self.__rg = rg
        self.__cpf = cpf
        self.telefone = telefone

    def get_cpf(self):
        return self.__cpf

    def set_cpf(self, cpf):
        self.__cpf = cpf
        return self.__cpf

    def get_rg(self):
        return self.__rg

    def set_rg(self, rg):
        self.__rg = rg
        return self.__rg

    def exibir_dados():


class Medico(Pessoa):
    def __init__(self, nome, rg, cpf, telefone, crm, salario, especialidade):
        super().__init__(nome, rg, cpf, telefone)
        self.crm = crm
        self.salario = salario
        self.especialidade = especialidade

    def dados_medico(self):
        super().exibir_dados()
        print('Telefone: ', self.telefone)
        print('CRM: ', self.crm)
        print('Salário: ', self.salario)
        print('Especialidade: ', self.especialidade)


class Paciente(Pessoa):
    def __init__(self, nome, rg, cpf, telefone, endereco, nascimento):
        super().__init__(nome, rg, cpf, telefone)
        self.endereco = endereco
        self.nascimento = nascimento

medico_01 = Medico('Fulano de Tal', 12345647, 99933366645,
                   1199553644, '1111/SP', 2500.50, 'Ortopedista')
medico_01.dados_medico()
  • Yes, I have been studying about private attributes and how to access them from another class. Thank you very much,

1 answer

3


The biggest reason it doesn’t work is that the display method doesn’t have a parameter to receive the object, in this case the self as used in other methods.

class Pessoa:
    def __init__(self, nome, rg, cpf, telefone):
        self.nome = nome
        self.__rg = rg
        self.__cpf = cpf
        self.telefone = telefone
    
    def get_cpf(self):
        return self.__cpf
    
    def set_cpf(self, cpf):
        self.__cpf = cpf
        return self.__cpf
    
    def get_rg(self):
        return self.__rg
    
    def set_rg(self, rg):
        self.__rg = rg
        return self.__rg
    
    def exibir_dados(self):
        pass

class Medico(Pessoa):
    def __init__(self, nome, rg, cpf, telefone, crm, salario, especialidade):
        super().__init__(nome, rg, cpf, telefone)
        self.crm = crm
        self.salario = salario
        self.especialidade = especialidade
    
    def dados_medico(self):
        super().exibir_dados()
        print('Telefone: ', self.telefone)
        print('CRM: ', self.crm)
        print('Salário: ', self.salario)
        print('Especialidade: ', self.especialidade)

class Paciente(Pessoa):
    def __init__(self, nome, rg, cpf, telefone, endereco, nascimento):
        super().__init__(nome, rg, cpf, telefone)
        self.endereco = endereco
        self.nascimento = nascimento

medico_01 = Medico('Fulano de Tal', 12345647, 99933366645, 1199553644, '1111/SP', 2500.50, 'Ortopedista')
medico_01.dados_medico()

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

But I don’t understand what having private camps has to do with data being sensitive. Do you happen to believe that you are creating some data protection by doing this? If so, completely review your idea of security. Also having private fields is not encapsulation, look for good materials on the subject, here on the site has.

There seem to be conceptual errors in the code, but I can’t say without fully understanding the problem.

Browser other questions tagged

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