我想对对象的所有实例的 costsum 属性求和。
class ActivityCenter:
def __init__(self, costpool, costsum, costdriver, cdunits):
self.costpool = costpool
self.costsum = costsum
self.costdriver = costdriver
self.cdunits = cdunits
cp1 = ActivityCenter("Material Handling", 480000, "Pounds", 160000)
cp2 = ActivityCenter("Production orders", 90000, "Num of POs", 200)
# I am looking to add up the costsum values for all instances, similar to:
costsumtotal = (cp1.__dict__.get("costsum")) + (cp2.__dict__.get("costsum"))
到目前为止,我已经尝试使用 sum() 理解如下,参考这个解决方案:
B = []
for i in range(10):
B.append(ActivityCenter())
s = sum(i.costsum for i in B)
但是我无法克服缺少 4 个必需的位置参数的 TypeError。