我試圖追加一個項目到一個外部類的參數是一個列表,但我需要創建一個實例。但是,如何在不使用相同的類實例的情況下爲該類的參數創建一個實例?這裏是我想要做的一個小例子調用一個類的__init__屬性並追加到該屬性
class A(object):
def __init__(self, list1, arg1)
self.list1 = list1
self.arg1 = arg1
class B(object):
def __init__(self, arg2, arg3)
self.arg2 = arg2
self.arg3 = arg3
def method1(self, arg4)
self.arg4 = arg4
if 0 < 1:
A(A.list1, A.arg1).list1.append('newitem')
return list1
我應該怎麼做才能將newItem追加到list1?
如果我不好好解釋自己,我很抱歉。我真的很困惑
EDIT(加入我的實際代碼)
class SimpleVirus(object):
def __init__(self, maxBirthProb, clearProb):
self.maxBirthProb = maxBirthProb
self.clearProb = clearProb
def reproduce(self, popDensity):
self.popDensity = popDensity
reproduceProb = self.getMaxBirthProb() * (1 - popDensity)
child = SimpleVirus(self.getMaxBirthProb(), self.getClearProb())
VirusL = Patient.getVirusesList()
if random.random() <= reproduceProb:
#Down here is where I need to append to the "viruses" list
Patient(viruses, maxPop).viruses.append(child)
return child
else:
raise NoChildException()
class Patient(object):
def __init__(self, viruses, maxPop): #viruses is a list
self.viruses = viruses
self.maxPop = maxPop