Python의 객체 지향 프로그래밍
Alex Yarosh
Content Quality Analyst @ DataCamp

# Withdraw amount from each of accounts in list_of_accounts def batch_withdraw(list_of_accounts, amount): for acct in list_of_accounts: acct.withdraw(amount)b, c, s = BankAccount(1000), CheckingAccount(2000), SavingsAccount(3000) batch_withdraw([b,c,s]) # <-- Will use BankAccount.withdraw(), # then CheckingAccount.withdraw(), # then SavingsAccount.withdraw()
batch_withdraw()는 어떤 withdraw()를 호출할지 객체를 확인할 필요가 없습니다
기본 클래스는 프로그램의 어떤 특성도 바꾸지 않고 모든 하위 클래스로 대체 가능해야 합니다
BankAccount가 동작하는 곳이면 CheckingAccount도 동작해야 합니다

기본 클래스는 프로그램의 어떤 특성도 바꾸지 않고 모든 하위 클래스로 대체 가능해야 합니다
→ 구문 비호환
BankAccount.withdraw()는 매개변수 1개가 필요하지만, CheckingAccount.withdraw()는 2개가 필요함
→ 하위 클래스가 입력 조건 강화
BankAccount.withdraw()는 어떤 금액이든 허용하지만, CheckingAccount.withdraw()는 금액에 제한을 둠
→ 하위 클래스가 출력 조건 약화
BankAccount.withdraw()는 양의 잔액만 남기거나 오류를 발생시킬 수 있으나, CheckingAccount.withdraw()는 잔액을 음수로 둘 수 있음
→ 하위 클래스 메서드에서 추가 속성 변경
→ 하위 클래스 메서드에서 추가 예외 발생
$$\text{\textbf{\Huge{LSP 없으면 — 상속 없음}}}$$
Python의 객체 지향 프로그래밍