Search code examples
python-3.7

How can i grab some Parent Class Atribute values using Multiple Inheritance


I'm start learning Multiple Inheritance, but i can't seem to grab some Atribute values from Parent Classes.

I have 3 Classes and one of them inherits the other two. I can print the "preco_bilhete" Attribute, but not "nome", "apelido" and "codigo_voo" Attribute values!

I'm sorry for some of my code is in Portuguese.

class Pessoa():
  def __init__(self, nome, apelido, idade, cc, nacionalidade):
    self.nome = nome
    self.apelido = apelido
    self.idade = idade
    self.cartaocidadao = cc
    self.nacionalidade = nacionalidade


class Voo():
  def __init__(self, companhia, cod_voo, cod_aviao, data_partida, horario_partida, data_chegada, horario_chegada, aeroporto_partida, terminal_aeroporto_partida,
           aeroporto_chegada, terminal_aeroporto_chegada, tipo_de_bagagem):
    self.companhia_aerea = companhia
    self.codigo_aviao = cod_aviao
    self.codigo_voo = cod_voo
    seld.data_voo_partida = data_partida
    self.horario_partida = horario_partida
    seld.data_voo_chegada = data_chegada
    self.horario_chegada = horario_chegada
    self.aeroporto_partida = aeroporto_partida
    self.terminal_aeroporto_partida = terminal_aeroporto_partida
    self.aeroporto_chegada = aeroporto_chegada
    self.terminal_aeroporto_chegada = terminal_aeroporto_chegada
    self.tipo_de_bagagem = tipo_de_bagagem


class Comprar_Bilhete(Pessoa, Voo):
  def __init__(self, nome, apelido, idade, cc, nacionalidade, companhia, cod_voo, cod_aviao, data_partida, horario_partida, data_chegada, horario_chegada,
           aeroporto_partida, terminal_aeroporto_partida, aeroporto_chegada, terminal_aeroporto_chegada, tipo_de_bagagem, preco):
    self.preco_bilhete = preco


cliente1 = Comprar_Bilhete("Pedro", "Figueiredo", 49, 9876543, "Portuguesa", "Easyjet", "EJ1011", "FT4537", "27-08-2020", "23:05", "28-08-2020", "01:45",
                       "Humberto Delgado - Lisboa - PT", "Terminal 1", "Stansted - Hertfordshire - UK", "Terminal 3", "Bagagem de Porão + Mala de Mão", 275.48)

print(cliente1.preco_bilhete)

print(cliente1.nome)
print(cliente1.apelido)
print(cliente1.codigo_voo)

Solution

  • Since it is multiple inheritance, here, your class Comprar_Bilhete(Pessoa, Voo) is inheriting the 2 classes Pessoa and Voo. When one class inherits another class, it also inherits it's variables and functions (since access levels of both by default is public in python).

    You are only able to print the value of the variable: preco_bilhete as it is the only one being initialised in your base class - Comprar_Bilhete. To be able to get a value on printing the other 3 derived variables i.e. nome , apelido and codigo_voo, you need to pass values to initialise them as well.

    This seems to fix the error you are encountering:

    class Comprar_Bilhete(Pessoa, Voo):
      def __init__(self, nome, apelido, idade, cc, nacionalidade, companhia, cod_voo, cod_aviao, data_partida, horario_partida, data_chegada, horario_chegada,
               aeroporto_partida, terminal_aeroporto_partida, aeroporto_chegada, terminal_aeroporto_chegada, tipo_de_bagagem, preco):
        self.preco_bilhete = preco
        self.nome = nome
        self.apelido =apelido
        self.codigo_voo = cod_voo