class BankAccount:def__init__(self, account_holder: str, balance: int=0):self.account_holder = account_holderself.balance = balancedef deposit(self, amount: int):self.balance += amountdef withdraw(self, amount: int):if amount <=self.balance:self.balance -= amountreturnTrueelse:returnFalse# Comparison based on account balancedef__lt__(self, other: "BankAccount") ->bool:returnself.balance < other.balancedef__eq__(self, other: object) ->bool:ifnotisinstance(other, BankAccount):raiseNotImplementedErrorreturnself.balance == other.balancedef main() ->None: account1 = BankAccount("Alice", 1000) account2 = BankAccount("Bob", 1500)print(account1 < account2) # True, because Alice's balance is less than Bob'sprint(account1 == account2) # False, because their balances are not equalif__name__=="__main__": main()
True
False
Dataclass
In normal behavior-focused class, we need to pass in the variables to the initializer (__init__) and also store that in the instance, which is a duplication and extra code that we dont want to have.