1. 학습
  2. /
  3. 강의
  4. /
  5. Python 중급 객체 지향 프로그래밍

Connected

연습 문제

balance 속성에 접근하기

이 연습 문제에서는 @property 데코레이터를 사용해 디스크립터를 구현한 BankAccount 클래스의 balance 속성에 접근하는 방법을 연습해 봅니다. 아래에 보이는 대로 BankAccount 클래스가 이미 준비되어 있어요:

class BankAccount:
  def __init__(self, balance):
    self.balance = balance

  @property
  def balance(self):
    return f"${round(self._balance, 2)}"

  @balance.setter
  def balance(self, new_balance):
    if new_balance > 0:
      self._balance = new_balance

  @balance.deleter
  def balance(self):
    print("Deleting the 'balance' attribute")
    del self._balance

지침

100 XP
  • 새로 만든 checking_account 객체의 balance 속성을 print()로 출력하세요.
  • balance 값을 150으로 설정한 뒤, 갱신된 속성을 다시 출력하세요.
  • checking_account 객체에서 balance 속성을 삭제하세요.