1. Learn
  2. /
  3. Courses
  4. /
  5. Python 中級オブジェクト指向プログラミング

Connected

Exercise

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

Instructions

100 XP
  • 新しく作成した checking_account オブジェクトの balance 属性を、print() を使って出力します。
  • balance の値を 150 に設定し、更新後の属性をもう一度出力します。
  • checking_account オブジェクトから balance 属性を削除します。