1

I would like to know if this is correctly done, I want to insert the data of the attributes of the material object in a list. In the function of the material object, I give value to the material attributes and then add this data in a list that will do so in the library function.

I want to give value to an object and add its values ​and be able to show the result of the list

Also when I print the list the message that comes out is this [<__ main __. Material object at 0x03994ED0>]

class Biblioteca():
    def cargarMateriales(self,material,a):
        material.append(a)
        return material

class Material(Biblioteca):
    def __init__(self,tipoMaterial=None,codigo=None,autor=None,titulo=None,anio=None,status=None):
        self.tipoMaterial = tipoMaterial
        self.codigo = codigo
        self.autor = autor
        self.titulo = titulo
        self.anio = anio
        self.status = status
    def __str__(self):
        return "Tipomaterial {0}, codigo {1}, autor {2}, titulo {3}, anio {4}, status {5}".format(self.tipoMaterial,self.codigo,self.autor,self.titulo,self.anio,self.status)
    def altaMaterial(self):
        self.tipoMaterial = input(str("tipo"))
        self.codigo = input(str("codigo"))
        self.autor = input(str("autor"))
        self.titulo = input(str("titulo"))
        self.anio = int(input("anio"))
        self.status = input(str("status"))
material = []
a = Material()
a.altaMaterial()
material.append(a)
print(material)
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
Oriol
  • 147
  • 6

1 Answers1

1

Replace __str__ with __repr__ like this:

def __repr__(self):
        return "Tipomaterial {0}, codigo {1}, autor {2}, titulo {3}, anio {4}, status {5}".format(self.tipoMaterial,self.codigo,self.autor,self.titulo,self.anio,self.status)

also if you are using Python3.6 or higher, you can use new form of formated string

def __repr__(self):
        return f'Tipomaterial {self.tipoMaterial},codigo {self.codigo}, autor {self.autor}, titulo {self.titulo}, anio {self.anio}, status {elf.status}'
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59