案例研究:用 Python 构建软件
Mark Pedigo, PhD
Principal Data Scientist

BasicCalculatorFinancialCalculatorBasicCalculator 的子类MortgageCalculatorFinancialCalculatordef MortgageClass(FinancialCalculator):
"""
MortgageClass 扩展 FinancialCalculator,用于按揭相关计算。
"""
...
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
# 创建一个属性以保存每月还款额。
self.monthly_payment = self.calculate_monthly_payment()
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 构建软件