1. Učit se
  2. /
  3. Kurzy
  4. /
  5. Intermediate Object-Oriented Programming in Python

Connected

cvičení

Přístup k atributu balance

V tomto cvičení si procvičíš přístup k atributu balance třídy BankAccount, která implementuje deskriptor pomocí dekorátoru @property. Třída BankAccount je pro tebe již připravená, jak je znázorněno níže:

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

Pokyny

100 XP
  • Vypiš pomocí print() hodnotu atributu balance nově vytvořeného objektu checking_account.
  • Nastav hodnotu balance na 150 a znovu vypiš aktualizovaný atribut.
  • Odstraň atribut balance z objektu checking_account.