Simple product inventory project The goal:


  Create an application which manages an inventory of products. Create a product class which has a price, id, and quantity on hand. Then create an inventory class which keeps track of various products and can sum up the inventory value.


I figured out how to get a list of dictionaries, and then add up the price. My output isn't pretty, but before I continue, I would simply like general critique.

class Product:
    def __init__(self, i, p, q):
        self.identification = i
        self.price = p
        self.quantity = q
        self.item_dictionary = {'ID: ': self.identification, 'Price: ': self.price, 'Quantity: ': self.quantity}


class Inventory:
    def __init__(self):
        self.identification, self.price_list, self.quantity_list = [], [], []
        self.products = [
            product_1.item_dictionary,
            product_2.item_dictionary
        ]  # Make list of item dictionaries

    for i in self.products:
        self.identification.append(i['ID: '])
        self.price_list.append(i['Price: '])
        self.quantity_list.append(i['Quantity: '])
        # Make unique lists of ID, Price, and Quantity

    print(sum(self.price_list))


product_1 = Product(32, 23, 2)
product_2 = Product(23, 65, 3)


inventory_1 = Inventory()
Single letter names are not readable in general. i, p and q, for example, would be easier to read as identification, price, and quantity. This is also a common pattern, where parameters to be stored as fields are named the same as the fields.
The last three lines should be in a main function, called if __name__ == '__main__'.
item_dictionary would be better expressed either by making Product a dict subtype or making an as_dict method.
product_1 and product_2 should be passed as either a products array argument or a *products star argument to Inventory().
Why unpack the products in Inventory?
The code directly inside Inventory (for loop and print()) should be in a method. I've never even seen Python code with something other than method definitions at the top level.
Product quantity is a property of an inventory, not a product.


I would probably implement this as follows (untested):

class Product:
    def __init__(self, identification, price):
        self.identification = identification
        self.price = price

class Inventory:
    def __init__(self, products):
        self.products = products

    @property
    def value():
        return sum(product.price for product in self.products)

def main():
    product_1 = Product(32, 23)
    product_2 = Product(23, 65)
    inventory = Inventory(2 * [product_1] + 3 * [product_2])
    print(inventory.value)

if __name__ == '__main__':
    main()