- Single letter names are not readable in general.
i
,p
andq
, for example, would be easier to read asidentification
,price
, andquantity
. 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, calledif __name__ == '__main__'
. item_dictionary
would be better expressed either by makingProduct
adict
subtype or making anas_dict
method.product_1
andproduct_2
should be passed as either aproducts
array argument or a*products
star argument toInventory()
.- Why unpack the products in
Inventory
? - The code directly inside
Inventory
(for
loop andprint()
) 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([productproduct.price for product in self.products]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()