為新類別撰寫文件

案例研究:用 Python 建構軟體

Mark Pedigo, PhD

Principal Data Scientist

路線圖

專案路線圖:已完成與即將開始的部分。

案例研究:用 Python 建構軟體

你的類別階層

  • BasicCalculator
    • 執行基本運算
  • FinancialCalculator
    • BasicCalculator 的子類別
    • 增加利率等財務功能
  • MortgageCalculator
    • 繼承自 FinancialCalculator
    • 結合兩個類別的功能
案例研究:用 Python 建構軟體

Docstrings

  • docstring 的好處
    • 為程式碼提供清楚、精簡的說明
  • 如何建立 docstring
    • 在第一行加入字串常值
    • 說明目的與功能
def MortgageClass(FinancialCalculator):
    """
    MortgageClass extends the FinancialCalculator to mortgage specific calculations.
    """
    ...
案例研究:用 Python 建構軟體

為 Mortgage Calculator 類別加入屬性

    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()
案例研究:用 Python 建構軟體

追蹤類別功能

  • 使用 dir(ClassName) 取得類別的函式清單
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']
案例研究:用 Python 建構軟體

一起來練習吧!

案例研究:用 Python 建構軟體

Preparing Video For Download...