【发布时间】:2023-04-11 10:39:02
【问题描述】:
这就是我想要做的 在类中调用静态方法来填充类变量。
import sys
import os
from HelpingData import *
class Inventory(object):
shipping_cost = 400.0
total_stock = calculate_total_stock.__func__()
def __init__(self, attributes={}):
self.inventory = {}
if attributes is None:
self.inventory = {}
else:
for key in attributes:
self.inventory[key] = attributes[key]
def getValue(self,attribute):
return self.inventory[attribute]
def setValue(self,attribute,value):
self.inventory[attribute]=value
@staticmethod
def calculate_total_stock():
total_stock = dict((item, 0) for item in product_names)
for nation in product_stock:
for item in nation:
total_stock[item] += nation[item]
return total_stock
这是我得到的错误..
> total_stock = calculate_total_stock.__func__()
NameError: name'calculate_total_stock' is not defined
有人可以建议我吗,我在这里缺少什么,我是 python 新手。 1 天大
【问题讨论】:
-
请检查您的缩进并提供一个复制错误的minimal example。但是请注意,您不能在定义之前调用
calculate_total_stock。 -
缩进就好了。我只给出了最少的代码。
-
请原谅我的编辑,当我在这里发布时格式发生了变化,在我的代码中缩进是正确的..
-
请检查代码,现在告诉我你看到了什么错误
-
预注释,调用前移动函数声明没有帮助,错误来了,库存未定义..(即)类名
标签: python error-handling static-methods