Python:从列表理解中更改实例的值

Python: Changing the value of an instance from a list comprehension

目前正在自学 python 我正在尝试制作一个简单的支付脚本,我在这里遇到了一些障碍,我尝试使用一个函数来构建一个简单的发送支付在两个客户之间使用列表理解

 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]

在我尝试查看两个客户的新余额是否已更新之前,此方法运行良好,正如您在下面看到的,我 运行 A.balance 在我 运行 之后函数 sendpayment,两个客户实例没有任何变化。我希望实现的是,一旦此函数为 运行.

,两个属性的平衡就会发生变化
>>> A = Account("Alice", 100)
>>> B = Account("Bob", 50)
>>> Account.sendpayment("Alice", "Bob", 10)
90
60
>>> A.balance
100
>>> B.balance
50

下面是代码的其余部分,因此您可以大致了解脚本中客户和帐户的其余部分 类。

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]
def sendpayment(sender, recipient, amount):
    #first fetch the sender and recipient accounts
    '''Previously had 
    for s in Account.accountList:
        if s.name == sender:
            break
    for r in Account.accountList:
        if r.name == recipient:
            break
    '''
    s = filter(lambda x: x.name == sender, Account.accountList)[0]
    r = filter(lambda x: x.name == recipient, Account.accountList)[0]
    r.balance += amount
    s.balance -= amount
    print(r.balance, s.balance)