newbie question: help with polymorphism and/or programming style

adina_levin at mindspring.com adina_levin at mindspring.com
Sun Sep 30 14:21:57 EDT 2001


Hello, Pythonites.

I am teaching myself python programming, and have created a toy storefront
program.

If the customer wants to pay with cash, the program should return change. If
the customer wants to pay using a credit card, the program should validate
the customer's ability to pay, and return an approval.

Several questions regarding programming style:

a) Is it desirable to replace the "if" statement in paymenthandler with
subclasses, PayCash(Payment) and PayCredit(Payment).  If so, doesn't that
just push the "if" statement upstream, where you PayCash() if the customer
says she wants to pay cash?  I am clearly missing something!! :-(

b) Is it bad style to return different kinds of information, depending on
whether the customer pays with cash or credit? If so, how should this be
handled cleanly?

c) Any other programming style problems?

This is a toy, so no reason to comment on lack of full detail in the
simulation.

class Payment:
    def __init__(self):
        self.payobject=Customer().getpaymentinput()

    def payhandler(self,sum):
        self.sum=sum
        if self.payobject.paytype == "cash":
            result=self.payobject.cashamount-self.sum
            print result

        elif self.payobject.paytype == "credit":
            self.crednum=self.payobject.crednum
            result=Creditcheck().creditapprove(self)

        return result







More information about the Python-list mailing list