Доступ до атрибуту balance
У цій вправі ви попрактикуєтеся в доступі до атрибуту balance класу BankAccount, який реалізує дескриптор за допомогою декоратора @property. Клас 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
Ця вправа є частиною курсу
Середній рівень об'єктно-орієнтованого програмування в Python
Інструкції до вправи
- Виведіть атрибут
balanceщойно створеного об'єктаchecking_accountза допомогоюprint(). - Присвойте значення
balanceрівним150і знову виведіть оновлений атрибут. - Видаліть атрибут
balanceз об'єктаchecking_account.
Інтерактивна практична вправа
Спробуйте виконати цю вправу, доповнивши цей зразок коду.
checking_account = BankAccount(100)
# Output the balance of the checking_account object
print(____.____)
# Set the balance to 150, output the new balance
____.____ = ____
print(____.____)
# Delete the balance attribute, attempt to print the balance
____ checking_account.____