Documenting your new class

Case Study: Building Software in Python

Mark Pedigo, PhD

Principal Data Scientist

Roadmap

Roadmap showcasing what parts of the project we've finished and which we'll be starting.

Case Study: Building Software in Python

Your Class Hierarchy

  • BasicCalculator
    • Performs basic arithmetic
  • FinancialCalculator
    • Child of BasicCalculator
    • Adds financial features, such as interest rate
  • MortgageCalculator
    • Inherits from FinancialCalculator
    • Combines functionality of both classes
Case Study: Building Software in Python

Docstrings

  • Benefits of docstrings
    • Provides clear, concise explanations of code
  • How to create a docstring
    • Add a string literal as the first statement
    • Write an explanation of purpose and functionality
def MortgageClass(FinancialCalculator):
    """
    MortgageClass extends the FinancialCalculator to mortgage specific calculations.
    """
    ...
Case Study: Building Software in Python

Add attributes to the Mortgage Calculator class

    class MortgageCalculator(FinancialCalculator):
    def __init__(self, loan_amount, annual_interest_rate, years):
        super().__init__()
        self.loan_amount = loan_amount
        self.monthly_interest_rate = self.monthly_interest(annual_interest_rate)
    self.months = years * 12
    # Create an attribute to hold the monthly payment amount.
    self.monthly_payment = self.calculate_monthly_payment()
Case Study: Building Software in Python

Keep Track of Functionality of Classes

  • Use dir(ClassName) to get a list of the functions of a class
dir(MortgageCalculator)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', 
'__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', 
'difference', 'monthly_interest', 'months_from_years',
'power', 'product', 'quotient', 'sum']
Case Study: Building Software in Python

Let's practice!

Case Study: Building Software in Python

Preparing Video For Download...