【发布时间】:2019-03-08 07:51:22
【问题描述】:
我有一个方法对象,该方法对象从类中的用户输入中分配了值。问题是我不能在课堂外使用方法对象maxcount_inventory = int(input("How many Inventories: "))。错误显示“method' object cannot be interpreted as an integer”
class CLASS_INVENTORY:
maxcount_inventory = int(input("How many Inventories: "))
inventory_name = []
def __init__(Function_Inventory):
for count_inventory in range(Function_Inventory.maxcount_inventory):
add_inventory = str(input("Enter Inventory #%d: " % (count_inventory+1)))
Function_Inventory.inventory_name.append(add_inventory)
def Return_Inventory(Function_Inventory):
return Function_Inventory.inventory_name
def Return_Maxcount(Function_Inventory):
return maxcount_inventory
maxcount_inventory = CLASS_INVENTORY().Return_Maxcount
如果可以的话,还有一个额外的问题,我如何访问类外每个索引的列表中的项目?我有下面的代码,但我认为它不起作用。由于我上面的错误,还没有发现。
for count_inventory in range(maxcount_inventory):
class_inv = CLASS_INVENTORY().Return_Inventory[count_inventory]
print(class_inv)
skip()
这是我的完整代码:https://pastebin.com/crnayXYy
【问题讨论】:
-
您忘记为带有 maxcount_inventory 变量的 return_maxcount 提供参数
-
你为什么使用名称
Function_Inventory作为方法的参数?使用self几乎是不可侵犯的标准,否则你会混淆你自己和阅读你代码的任何人。 -
@DanielRoseman 我明白了。好的,我会切换到“自我”。
-
@mgracer 如何提供参数?
-
你不需要参数,你需要实际调用方法:
CLASS_INVENTORY().Return_Maxcount()。
标签: python python-3.x list function class