我试图追加一个项目到一个外部类的参数是一个列表,但我需要创建一个实例。但是,如何在不使用相同的类实例的情况下为该类的参数创建一个实例?这里是我想要做的一个小例子调用一个类的__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