Acessando o atributo balance
Neste exercício, você vai praticar o acesso ao atributo balance de uma classe BankAccount que implementa um descritor usando o decorator @property. A classe BankAccount foi criada para você, conforme mostrado abaixo:
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
Este exercício faz parte do curso
Programação Orientada a Objetos Intermediária em Python
Instruções do exercício
- Mostre na saída o atributo
balancedo objetochecking_accountrecém-criado usandoprint(). - Defina o valor de
balancecomo150e mostre novamente o atributo atualizado. - Exclua o atributo
balancedo objetochecking_account.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
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.____