Introduction To Computation and Programming Using Python, Revised - Guttag, John v..126
The document discusses an abstract Mortgage class that contains methods shared by subclasses but is not instantiated directly. It includes a findPayment method that uses a closed-form expression to calculate the monthly payment needed to pay off a loan over its term. When incorporating formulas from other sources, the variables and implementation should be fully understood and tested against examples. The Mortgage class initializes attributes for the loan amount, interest rate, months, and empty lists to track payments and balances over time.
Introduction To Computation and Programming Using Python, Revised - Guttag, John v..126
The document discusses an abstract Mortgage class that contains methods shared by subclasses but is not instantiated directly. It includes a findPayment method that uses a closed-form expression to calculate the monthly payment needed to pay off a loan over its term. When incorporating formulas from other sources, the variables and implementation should be fully understood and tested against examples. The Mortgage class initializes attributes for the loan amount, interest rate, months, and empty lists to track payments and balances over time.
Figure 8.8 contains the abstract class Mortgage. This class contains methods that are shared by each of the subclasses, but it is not intended to be instantiated directly. The function findPayment at the top of the figure computes the size of the fixed monthly payment needed to pay off the loan, including interest, by the end of its term. It does this using a well-known closed-form expression. This expression is not hard to derive, but it is a lot easier to just look it up and more likely to be correct than one derived on the spot. When your code incorporates formulas you have looked up, make sure that:
You have taken the formula from a reputable source. We looked at
multiple reputable sources, all of which contained equivalent formulas.
You fully understand the meaning of all the variables in the formula.
You test your implementation against examples taken from reputable
sources. After implementing this function, we tested it by comparing our results to the results supplied by a calculator available on the Web.
def findPayment(loan, r, m):
"""Assumes: loan and r are floats, m an int Returns the monthly payment for a mortgage of size loan at a monthly rate of r for m months""" return loan*((r*(1+r)**m)/((1+r)**m - 1)) class Mortgage(object): """Abstract class for building different kinds of mortgages""" def __init__(self, loan, annRate, months): """Create a new mortgage""" self.loan = loan self.rate = annRate/12.0 self.months = months self.paid = [0.0] self.owed = [loan] self.payment = findPayment(loan, self.rate, months) self.legend = None #description of mortgage def makePayment(self): """Make a payment""" self.paid.append(self.payment) reduction = self.payment - self.owed[-1]*self.rate self.owed.append(self.owed[-1] - reduction) def getTotalPaid(self): """Return the total amount paid so far""" return sum(self.paid) def __str__(self): return self.legend
Figure 8.8 Mortgage base class
Looking at __init__, we see that all Mortgage instances will have instance variables corresponding to the initial loan amount, the monthly interest rate, the duration of the loan in months, a list of payments that have been made at the start of each month (the list starts with 0.0, since no payments have been made at the start of the first month), a list with the balance of the loan that is