首页 > 解决方案 > AttributeError:“list”对象没有属性“drug_name”(类属性)

问题描述

class Prescription:
    def __init__(self, patient, drug_name, quantity):
        self.patient = patient
        self.drug_name = drug_name
        self.quantity = quantity

    def __repr__(self):
        return 'Prescription(\'' + self.patient + '\', \'' + self.drug_name + 
        '\', ' + \
        str(self.quantity) + ')'

在一个单独的文件中,我有:

from lab10_classes import Pharmacy, Prescription


def batch_dispense(pharmacy, prescriptions):
    total_cost = 0
    if len(prescriptions) == 0:
        return 0
    for i in prescriptions:
        name = i.drug_name
        if name not in pharmacy.inventory:
            continue
        elif name in pharmacy.inventory:
            if pharmacy.inventory[name] > i.quantity:
                pharmacy.inventory[name] = pharmacy.inventory[name] - 
                prescriptions.quantity
                total_cost += pharmacy.unit_prices * prescriptions.quantity
    return total_cost and pharmacy


Print([Prescription('Mack', 'Zocor', -27), Prescription('Rachael', 'Crestor', 
      474), Prescription('Janet', 'Adderall', 60)])

在我正在进行的当前实验室中,我的一个函数参数称为“处方”,它需要一个处方对象列表,例如上面的打印语句。但是,我不断得到:

AttributeError:“list”对象没有属性“drug_name”

从理论上讲,我的处方参数应该是有效的,因为它具有处方类的实例类对象列表。

标签: pythonclasstypeerror

解决方案


推荐阅读