Dokumentiere deine neue Klasse

Fallstudie: Software in Python entwickeln

Mark Pedigo, PhD

Principal Data Scientist

Roadmap

Roadmap, die zeigt, welche Teile des Projekts fertig sind und welche wir starten.

Fallstudie: Software in Python entwickeln

Deine Klassenhierarchie

  • BasicCalculator
    • Führt Grundrechenarten aus
  • FinancialCalculator
    • Kind von BasicCalculator
    • Fügt Finanzfunktionen hinzu, z. B. Zinssatz
  • MortgageCalculator
    • Erbt von FinancialCalculator
    • Kombiniert die Funktionalität beider Klassen
Fallstudie: Software in Python entwickeln

Docstrings

  • Vorteile von Docstrings
    • Bieten klare, knappe Erklärungen zum Code
  • So erstellst du einen Docstring
    • Füge als erste Anweisung ein String-Literal hinzu
    • Erkläre Zweck und Funktionalität
def MortgageClass(FinancialCalculator):
    """
    MortgageClass extends the FinancialCalculator to mortgage specific calculations.
    """
    ...
Fallstudie: Software in Python entwickeln

Attribute zur Klasse Mortgage Calculator hinzufügen

    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()
Fallstudie: Software in Python entwickeln

Behalte die Funktionalität von Klassen im Blick

  • Verwende dir(ClassName), um eine Liste der Funktionen einer Klasse zu erhalten
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']
Fallstudie: Software in Python entwickeln

Lass uns üben!

Fallstudie: Software in Python entwickeln

Preparing Video For Download...