1. Nauka
  2. /
  3. Kursy
  4. /
  5. Programowanie obiektowe w Pythonie – poziom średnio zaawansowany

Connected

ćwiczenie

Dostęp do atrybutu balance

W tym ćwiczeniu przećwiczysz dostęp do atrybutu balance klasy BankAccount, która implementuje deskryptor za pomocą dekoratora @property. Klasa BankAccount została już przygotowana, jak pokazano poniżej:

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

Instrukcje

100 XP
  • Wyświetl atrybut balance nowo utworzonego obiektu checking_account za pomocą print().
  • Ustaw wartość balance na 150, a następnie ponownie wyświetl zaktualizowany atrybut.
  • Usuń atrybut balance z obiektu checking_account.