技术上下文在派生类中,我想在超类中使用列表的子集,并将其存储在变量中
商业背景:我运行了一个用药物治疗病毒患者的模拟。为了找出病人没有按照他们的处方用药会发生什么,我想随机选择一些处方药,只使用这些。因此,我使用以下名称:
TreatedPatient
服用所有药物的患者的父类NonCompliantPatient
一类没有maxPop
患者能容纳的最大病毒群takeProb
病人服用处方药的概率TreatedPatient.getPrescriptions()
函数返回处方药列表activeDrugs
变量来包含实际服用的药物我的代码
class NonCompliantPatient(TreatedPatient):
def __init__(self, viruses, maxPop, takeProb):
super(TreatedPatient, self).__init__(viruses, maxPop)
self.takeProb = takeProb
def update(self):
# Which drugs are actually taken?
activeDrugs = [drug for drug in self.getPrescriptions()
if random.random() < self.takeProb]
错误消息
C:\Knowledge\Python\ps4.py in update(self)
13 def update(self):
14 # Which drugs are actually taken?
---> 15 activeDrugs = [drug for drug in self.getPrescriptions() if random.random() < self.takeProb]
16
17 # Which viruses survive?
C:\Knowledge\Python\ps3b.pyc in getPrescriptions(self)
AttributeError: 'NonCompliantPatient' object has no attribute 'activeDrugs'
请解释activeDrugs
被视为属性的原因
目前没有回答
相关问题 更多 >
编程相关推荐