I have a class, Tariff, with a quantity and some prices:
class Tariff(): quantity = 0 item_1_price = 250 item_1_selected = False item_2_price = 350 item_2_selected = False item_3_price = 165 item_3_selected = False @property def total(self): return sum([ self.item_1_price * self.item_1_selected, self.item_2_price * self.item_2_selected, self.item_3_price * self.item_3_selected, ]) * self.quantity I can use it as such:
purchase = Tariff() purchase.quantity = 2 purchase.item_1_selected = True purchase.item_2_selected = True print(purchase.total) > 1200 I want a function that will tell me the total of an individual item. For example:
print(purchase.totals.item_1) > 500 What is the best way of going around this? Is this possible/easy?
total(singular), the other time you wrotetotals(plural). Are these supposed to be the same property, or two separate properties?Itemanamedtuple(or a class itself) and have a list of items instead?