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
Cet exercice fait partie du cours
Intermediate Object-Oriented Programming in Python
Instructions
- Output the
balanceattribute of the newly-createdchecking_accountobject usingprint(). - Set the value of
balanceto150and again output the updated attribute. - Delete the
balanceattribute from thechecking_accountobject.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
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.____