사용자 정의 클래스

케이스 스터디: 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-string 예시

name = "Alice"
age = 17
# f-string으로 변수 포함하기
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으로 소프트웨어 만들기

연습해 봅시다!

케이스 스터디: Python으로 소프트웨어 만들기

Preparing Video For Download...