Newbie learning OOP 2nd ?

wittempj@hotmail.com martin.witte at gmail.com
Mon May 30 09:19:02 EDT 2005


I would split your code such that the Q&A is seperated from the
calculations, and I would model the bill something like :

class allbills(object):
    def __init__(self, bill, tip):
        self._bill = bill
        self._tip = tip

    def gettip(self):
        return self._tip / 100.0
    tip = property(gettip)

    def gettotal(self):
        return self._bill * (1 + self.tax + self.tip)
    total = property(gettotal)

    def __repr__(self):
        return 'tip = %.2f, tax = %.2f, total = %.2f ' % \
               (self.tip, self.tax, self.total)

class usbill(allbills):
    def __init__(self, bill, tip):
        super(usbill, self).__init__(bill, tip)

    def gettax(self):
        return 0.0675
    tax = property(gettax)

class chicago_bill(allbills):
    def __init__(self, bill, tip):
        super(chicago_bill, self).__init__(bill, tip)

    def gettax(self):
        return 0.0725
    tax = property(gettax)




More information about the Python-list mailing list