カスタムクラス

ケーススタディ:Pythonでソフトウェアを構築する

Mark Pedigo

Principal Data Scientist

ロードマップ

プロジェクトの進捗と次に始める部分を示すロードマップ。

ケーススタディ:Pythonでソフトウェアを構築する

毎月のローン計算

毎月の住宅ローンの計算式は次のとおりです

$$\text{monthly payment} = P \cdot \frac{r (1 + r)^N}{(1+r)^N - 1},$$

ここで

  • $P$ は借入額、
  • $r$ は月利、
  • $N$ は支払回数(ヶ月)
1 https://en.wikipedia.org/wiki/Mortgage_calculator
ケーススタディ:Pythonでソフトウェアを構築する

クラスに機能を追加

月々の支払いを計算するメソッド

def monthly_payment(self):
    # 乗数を計算
    numerator = self.monthly_interest_rate * \
        (1 + self.monthly_interest_rate) ** self.months
    denominator = (1 + self.monthly_interest_rate) ** self.months - 1
    multiplier = self.divide(numerator, denominator)
    # 月々の支払いを算出
    monthly_payment = self.multiply(self.loan_amount, multiplier)
    return monthly_payment
ケーススタディ:Pythonでソフトウェアを構築する

月々の支払いを計算

# 住宅ローン計算機を生成
mortgage_calculator = calculators.MortgageCalculator(300000, 0.065, 30)

# 月々の支払いを計算
print(round(mortgage_calculator.monthly_payment, 2))
1703.37
ケーススタディ:Pythonでソフトウェアを構築する

dir() の再確認

  • 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__',
'calculate_monthly_payment', 'difference', 'monthly_interest',
'monthly_interest_rate', 'monthly_payment', 'months', 'power', 'principal',
'product', 'quotient', 'sum']
ケーススタディ:Pythonでソフトウェアを構築する

f文字列の例

name = "Alice"
age = 17
# f文字列で文に変数を埋め込む
greeting = f"Hello, my name is {name} and I'm {age} years old."
print(greeting)
Hello, my name is Alice and I'm 17 years old.
ケーススタディ:Pythonでソフトウェアを構築する

¡Vamos a practicar!

ケーススタディ:Pythonでソフトウェアを構築する

Preparing Video For Download...