Skip to content

Instantly share code, notes, and snippets.

@vubon
Created September 14, 2019 14:10
Show Gist options
  • Save vubon/4c0e133dc1e9a3761aeb6f8c5efe0c8b to your computer and use it in GitHub Desktop.
Save vubon/4c0e133dc1e9a3761aeb6f8c5efe0c8b to your computer and use it in GitHub Desktop.
Python Property function part 5
class Account:
""" A basic account information store class """
def __init__(self, bank_account: str, balance: float):
self.bank_account = bank_account
self.balance = balance
self._account_information = dict(bank_account=self.bank_account, balance=self.balance)
# Just declare the @property decorator and solve those problems.
@property
def account_information(self):
"""
Get account information
:return: {'bank_account': '0004-0067894712', 'balance': 1000.5}
:rtype: dict
"""
return self._account_information
@account_information.setter
def account_information(self, kwargs):
"""
:param kwargs:
:return:
"""
print("set a value")
self._account_information = kwargs
@account_information.deleter
def account_information(self):
"""
:return:
"""
print("call deleter method")
self._account_information = dict(bank_account=None, balance=None)
def __str__(self):
return f"Bank Account: {self.bank_account} - balance: {self.balance}"
account = Account("0004-0067894712", 1000.50)
print(f"account number: {account.bank_account}")
print(f"Current balance: {account.balance}")
print(f"Account information: {account.account_information}")
# Now set a new value in the _account_information attribute
account.account_information = {"bank_account": "0004-0067899870", "balance": 100.50}
# Output new account information
print(f"New account information: {account.account_information}")
# Output for deleter method
del account.account_information
# after deleting the information should print bank_account: None and balance=None
print(account.account_information)
# Output
"""
account number: 0004-0067894712
Current balance: 1000.5
Account information: {'bank_account': '0004-0067894712', 'balance': 1000.5}
set a value
New account information: {'bank_account': '0004-0067899870', 'balance': 100.5}
call deleter method
{'bank_account': None, 'balance': None}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment