为什么Python将我的变量视为属性?

2024-09-27 00:16:37 发布

您现在位置:Python中文网/ 问答频道 /正文

技术上下文在派生类中,我想在超类中使用列表的子集,并将其存储在变量中

商业背景:我运行了一个用药物治疗病毒患者的模拟。为了找出病人没有按照他们的处方用药会发生什么,我想随机选择一些处方药,只使用这些。因此,我使用以下名称:

  • 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被视为属性的原因


Tags: inself患者defrandom药物drugviruses

热门问题