balance 속성에 접근하기
이 연습 문제에서는 @property 데코레이터를 사용해 디스크립터를 구현한 BankAccount 클래스의 balance 속성에 접근하는 방법을 연습해 봅니다. 아래에 보이는 대로 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 중급 객체 지향 프로그래밍
연습 안내
- 새로 만든
checking_account객체의balance속성을print()로 출력하세요. balance값을150으로 설정한 뒤, 갱신된 속성을 다시 출력하세요.checking_account객체에서balance속성을 삭제하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
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.____