1. Learn
  2. /
  3. Courses
  4. /
  5. Intermediate Object-Oriented Programming in Python

Connected

Exercise

Accessing the balance attribute

In this exercise, you'll practice accessing the balance attribute of a BankAccount class that's implemented a descriptor using the @property decorator. The BankAccount class has been created for you, as shown below:

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
  • Output the balance attribute of the newly-created checking_account object using print().
  • Set the value of balance to 150 and again output the updated attribute.
  • Delete the balance attribute from the checking_account object.