从字典中查找某些值作为python构造函数的输入

2024-09-28 22:19:00 发布

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

因此,我有以下代码:

# constructor
def __init__(self, dict):

    # Inputs
    harvesting_time = 16 # hours
    recovery_rate = 90 # %
    unit_throughput = 93.63 # kg/annum/unit
    cost_per_unit = 275000 # $
    electricity = 0.0000331689825  # kWh/kg/unit
    energy_cost = 10.41  # cent/kWh
    labor_cost = 1777000  # $/annum
    concentration = 100 # g/L

    throughput = dict.get("output(kg/annum)",default=None)  # kg/annum
    carbon = dict.get("carbon",default=None)
    nitrogen = dict.get("nitrogen",default=None)
    carbohydrates = dict.get("carbohydrates",default=None)
    proteins = dict.get("proteins",default=None)
    lipids = dict.get("lipids",default=None)

其思想是假设另一个类生成一个值字典,该构造函数将其作为输入,并搜索这些值以进行某些计算。然而,每当我运行我的程序(外部),我得到以下错误

吞吐量=dict.get(“产量(千克/年)”,默认值=无)#千克/年

TypeError:get()不接受关键字参数

有人能解释一下这个错误是什么意思,我怎样才能避免它?多谢各位


Tags: nonedefaultgetunitdictkwhcarboncost
1条回答
网友
1楼 · 发布于 2024-09-28 22:19:00
throughput = dict.get("output(kg/annum)",default=None)  # kg/annum

指定默认值时不要使用关键字“default”

throughput = dict.get("output(kg/annum)", None)  # kg/annum

实际上,由于默认值是None,所以您根本不需要提供第二个参数

throughput = dict.get("output(kg/annum)")  # kg/annum

相关问题 更多 >