Can I create a list inside a python object?

Asked

Viewed 70 times

-1

have a question can I declare a list inside a Python object? If yes, I would do that, and how I would manipulate (add, print..) someone has some example or reference where they can study this?

guy..

class Veiculo(object):

    def __init__(self, id, valor, lista_id_vinculado[]):
        self.id=id
        self.valor=valor
        self.lista_id_vinculado[]=lista_id_vinculado[]
  • Hello and Gualberto, could you test my answer? In case it worked out, please mark as right. If you need help I’m available. Hug!

1 answer

0

In the following example I declared a list ex_proprietarios within the class Veiculo. The method add_ex_proprietarios adds names to the list. The method exibir_veiculo prints all attributes.

class Veiculo(object):

    ex_proprietarios = []


    def __init__(self, placa, modelo):

        self.placa = placa
        self.modelo = modelo


    def add_ex_proprietarios(self, nome):

        self.ex_proprietarios.append(nome)


    def exibir_veiculo(self):

        print(self.placa)
        print(self.modelo)
        print(self.ex_proprietarios)


carro = Veiculo("X1Y2Z3", "Opala")

carro.add_ex_proprietarios("Pedro")
carro.add_ex_proprietarios("Paulo")
carro.add_ex_proprietarios("Lucas")

carro.exibir_veiculo()

Exit

X1Y2Z3
Opala
['Pedro', 'Paulo', 'Lucas']

Browser other questions tagged

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