0

I have the following script:

 #we create a class for a product and its description

class Producto:
    def __init__(self, id_producto, linea_produccion, eval_numerica, eval_visual):
        self.id_producto = id_producto
        self.linea_produccion = linea_produccion
        self.eval_numerica = eval_numerica
        self.eval_visual = eval_visual



#here I enter product´s description
def lee_producto():
    id_producto = input()
    linea_produccion = input()
    eval_numerica = (float(input()))
    verificacion = input()
    eval_visual = ( verificacion == "s" )
    return Producto(id_producto, linea_produccion, eval_numerica, eval_visual)

#We call the previous function and add products
def lee_y_anade_producto(lista):
    nuevo_producto = lee_producto()
    lista.append(nuevo_producto)

#we call the previous function as many times as n
lista = []
n = int(input("cuantos productos vas a dar de alta:   ")) 
for i in range(n):
    lee_y_anade_producto(lista)

So far all that I have done is to feed information to my Class object, I don´t know how to do it backward, now that I have the information the way I want it How can I access to it, depending on the piece of information I want?

  1. Show all products in a line (that's self.linea_produccion a1, b2,c3) and show all items depending on what line I choose (a1, b2, c3)

. 2. And I need to calculate self.eval_numeric average of all items, regardless of everything else.

This is for learning purposes, I really want to understand how to and not just copy-paste.

3
  • I'm not sure I understand what you're asking, can you explain things differently, perhaps? Commented Mar 10, 2020 at 0:52
  • @AMC I know how to input data to a class object, now I need to extract that data Commented Mar 10, 2020 at 3:57
  • You just didn't know how to access the attributes of an object, is that all? Commented Mar 10, 2020 at 16:55

1 Answer 1

1

In your example you have lista which is a list of your Producto objects. This is what you have:

lista = []
n = int(input("cuantos productos vas a dar de alta:   ")) 
for i in range(n):
    lee_y_anade_producto(lista)

After this you can iterate through the list and get the attributes of each object like this:

for object in lista:
    id_producto = object.id_producto
    linea_produccion = object.linea_produccion

Anything that is set as self.attribute in __init__ can be called by object.attribute.

Sign up to request clarification or add additional context in comments.

4 Comments

But here I´m creating a list, using lista=[], and that list stores n products does that list keeps the same structure as the class object?
@RenéMartínez I've edited to answer your comment. Please try my suggestion and let me know if it works.
it did work, but I have anoter question, Why does print(lista.id_producto) gives error? shouldn´t that print all list´s id_producto?
@RenéMartínez lista is a list of objects so it does not have the attribute id_producto. You can access an individual object inside the list with an index, like this: lista[0].id_producto

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.