Teaching myself python at the moment and I am trying to make a simple payment script, I've hit a bit of a block here, I have tried to use a function to construct a simple send payment between two customers using list comprehension
def sendpayment(sender, recipient, amount):
[(print(x.balance - amount),print(y.balance + amount)) for x in Account.accountList
for y in Account.accountList
if x.name == sender and y.name == recipient]
This works well until I try to see if the new balance has been updated for the two customers, As you can see below once I run A.balance after i run the function sendpayment, Nothing changes on the two customer instances. What I was hoping to achieve was that the balance of the two attributes change once this function is run.
>>> A = Account("Alice", 100)
>>> B = Account("Bob", 50)
>>> Account.sendpayment("Alice", "Bob", 10)
90
60
>>> A.balance
100
>>> B.balance
50
Below is the rest of the code so you get a broad idea of the rest of the customer and account classes in the script.
class Customer:
def __init__(self, name, balance):
self.name = name
self.balance = balance
def __repr__(self):
return repr(self.__dict__)
def __getitem__(self, i):
return getattr(self, i, )
class Account:
accountList = []
def __init__(self, name, balance):
self.customer = Customer(name, balance)
Account.accountList.append(self)
def __repr__(self):
return repr(self.__dict__)
def __getitem__(self, i):
return getattr(self, i)
def __getattr__(self, attr):
return getattr(self.customer, attr)
def sendpayment(sender, recipient, amount):
[(print(x.balance - amount),print(y.balance + amount)) for x in Account.accountList
for y in Account.accountList
if x.name == sender and y.name == recipient]