【发布时间】:2021-04-12 17:19:55
【问题描述】:
我是编程和学习 Python 的新手。现在我正试图弄清楚如何在类内的方法定义中将值写入同一类的另一个对象。我的意思是这样的:
class myClass:
def __init__(self, attribute, lst = True):
self.attribute = attribute
if lst is True:
self.lst = []
def add_amount(self, amount):
self.lst.append(dict(Amount = amount))
def total:
self.total = []
for each in self.lst:
self.total.append(each.get("Amount"))
self.total = sum(self.total)
#def transfer(self, amount, attribute):
#here I would like to be able to add
#a value (for example self.total) to
#the lst [] of a different instance of
#this class
firstInstance = myClass("First")
secondInstance = myClass("Second")
firstInstance.add_amount(10)
firstInstance.add_amount(20)
##firstInstance.transfer(15, "Second")
#How can I write the transfer
#function so that it will add
#the value of the first argument
#to the empty list of the instance
#object with the attribute "Second"
#(in this case secondInstance)?
我该如何编程?我希望我能以一种可以理解的方式解释我的问题,希望你们能帮助我!提前致谢! :)
亲切的问候
【问题讨论】:
标签: python class object methods