0

I need to make a list that tracks the objects that have been called of a certain class. These objects need to be accessible and display information when called from the list.

pizza2 = DeluxePizza("small",1,1,1,1,1)
pizza2.__str__()
pizza3 = DeluxePizza("medium",1,2,3,0,2)
pizza3.__str__()
pizza4 = DeluxePizza("large",1,0,1,0,5)
pizza4.__str__()

Whenever a new object is called, they need to be stored in the list like this-

pizzas = [pizza2, pizza3, pizza4]
Timus
  • 10,974
  • 5
  • 14
  • 28
  • 1
    Does this answer your question? [Printing all instances of a class](https://stackoverflow.com/questions/328851/printing-all-instances-of-a-class) – jonrsharpe Oct 27 '20 at 14:20
  • No, not really, but thanks though. I want to store the instances as and how I call them in the main program in a list, which when I view later should allow me to access that instance. – Samveda Gundimeda Oct 27 '20 at 16:41
  • That's exactly what the answers on the dupe describe, how to automatically have a list of all of the instances. – jonrsharpe Oct 27 '20 at 16:54
  • I found my way around it through one of the answers. Thanks a lot! – Samveda Gundimeda Oct 29 '20 at 09:41

1 Answers1

0

I think you want to do something along the lines of the following:

pizza_orders = []
pizzas = [("small",1,1,1,1,1),("medium",1,2,3,0,2),("large",1,0,1,0,5)]
for pza in pizzas:
    pizza_orders.append(DeluxePizza("small",pza[0],pza[1],pza[2],pza[3],pza[4]))
for po in pizza_orders:
    print(po)

pizza_orders is a list of DeluxePizza Class instances pizzas is a list of the specific instantiations of the Deluxe Pizza class you want to order When you execute the po in pizza_orders list you will get the list of orders for the three pizzas

itprorh66
  • 3,110
  • 4
  • 9
  • 21
  • This does work, but if I keep calling more objects per se, how can I keep appending them into the list automatically when they're called? Or should I append the object itself? – Samveda Gundimeda Oct 27 '20 at 16:33
  • As I have shown just append the object itself. Of course if you need to refer to a specific object rather than iterate through all of the objects, you will either have to remember the index of the specific object or otherwise create a variable that is the object. – itprorh66 Oct 27 '20 at 18:26
  • I made them all append as objects in a list, it works somehow. Thanks! – Samveda Gundimeda Oct 29 '20 at 09:41
  • If you want to grow as a python programmer, you should probably figure out why it works. Not just accept the fact that it does, you will see this form many times in programming. – itprorh66 Oct 29 '20 at 13:31
  • What I did, was basically call the object through a function and provide the parameters for it through inputs. I did end up making two different lists, to store the objects and the attributes separately. – Samveda Gundimeda Oct 31 '20 at 06:33