balance attribute को access करना
इस अभ्यास में, आप @property डेकोरेटर का उपयोग करके डिस्क्रिप्टर लागू करने वाली BankAccount class के balance attribute तक पहुँचने का अभ्यास करेंगे. BankAccount class आपके लिए पहले से बनाई गई है, जैसा कि नीचे दिखाया गया है:
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
यह अभ्यास पाठ्यक्रम का हिस्सा है
Intermediate Object-Oriented Programming in Python
अभ्यास निर्देश
- नए बनाए गए
checking_accountobject केbalanceattribute कोprint()का उपयोग करके आउटपुट करें. balanceका मान150सेट करें और अपडेटेड attribute को फिर से आउटपुट करें.checking_accountobject सेbalanceattribute को डिलीट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
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.____