从字典将参数传递给类

时间:2019-11-03 02:32:05

标签: python class

我正在尝试将字典选择传递给类函数以进一步处理。但是,当我尝试尝试时,出现错误消息,提示我缺少参数。我知道我不需要包含自我,所以我对为什么它不起作用感到困惑。它应该做:

  1. 从接受用户输入的代码的其他部分接收choiceindex
  2. 使用choiceIndex从字典中进行选择
  3. 将字典的3个部分传递给Product类

项目用于模拟咖啡机。引用了一些我尝试过但不起作用的方法。任何帮助或建议都非常感谢

class Product(object):

    def __init__(self,name,price,recipe):
        self.name = name
        self.price = price
        self.recipe = recipe

    def getPrice(self):
        return self.price

    def make(self, name, price, recipe):
        print(self.recipe)
        #for item in recipe:
        #    print("dispensing", item)

class Selector(object):

    def __init__(self):
        #self.Product = Product()
        self.cashBox = CashBox
        self.product = []
        #self.products.append(Product.

    def select(self, choiceIndex):
        recipes = {
            1 : ["black","35","cup coffee water"],
            #1 : ["black",35,"cup coffee water"],
            #1 : self.Product.make("black",35,"cup coffee water"),
            2 : ["white",35,["cup", "coffee", "creamer", "water"]],
            3 : ["sweet",35,["cup", "coffee", "sugar", "water"]],
            4 : ["white&sweet",35,["cup", "coffee", "sugar", "creamer", "water"]],
            5 : ["bouillon",35,["cup bouillonPowder", "water"]]
        }
        if choiceIndex in range(1,len(recipes)+1):
            self.choiceIndex = choiceIndex
            self.recipe = recipes.get(choiceIndex)
            print(self.recipe,"Great selection")
            Product.make(*self.recipe)
        else:
            print("That selection does not exist")

发生错误:

Exception has occurred: TypeError
make() missing 1 required positional argument: 'recipe'
  File "C:\Users\Tanner Harmer\Desktop\Coffee2\CashBox.py", line 101, in select
    Product.make(*self.recipe)
  File "C:\Users\Tanner Harmer\Desktop\Coffee2\CashBox.py", line 41, in oneAction
    Selector.select(self,int(words[1]))
  File "C:\Users\Tanner Harmer\Desktop\Coffee2\CashBox.py", line 107, in main
    while m.oneAction():
  File "C:\Users\Tanner Harmer\Desktop\Coffee2\CashBox.py", line 113, in <module>
    main()

2 个答案:

答案 0 :(得分:0)

Product的实例具有namepricerecipe属性。

Product中,更改

def make(self, name, price, recipe):

def make(self):
    print(self.recipe)

Selector中,创建一个Product实例,然后调用其make方法。

更改

Product.make(*self.recipe)

product = Product(*self.recipe)    # make an instance of Product
product.make()    # call the instance method

答案 1 :(得分:0)

首先,产品类中的make方法应该是静态的,因此可以使用类名来调用它。即使您不将其设置为静态,也可以将第一个参数传递给make函数作为self,然后传递其余参数,但这没有任何意义,因为您正在使用类名调用make方法。

onPress

上面的代码产生以下输出

  

class Product(object): def __init__(self,name,price,recipe): self.name = name self.price = price self.recipe = recipe def getPrice(self): return self.price @staticmethod def make(name, price, recipe): print(recipe) for item in recipe: print("dispensing", item) class Selector(object): def __init__(self): #self.Product = Product() self.cashBox = 0 self.product = [] #self.products.append(Product. def select(self, choiceIndex): recipes = { 1 : ["black","35","cup coffee water"], 2 : ["white",35,["cup", "coffee", "creamer", "water"]], 3 : ["sweet",35,["cup", "coffee", "sugar", "water"]], 4 : ["white&sweet",35,["cup", "coffee", "sugar", "creamer", "water"]], 5 : ["bouillon",35,["cup bouillonPowder", "water"]] } if choiceIndex in range(1,len(recipes)+1): self.choiceIndex = choiceIndex self.recipe = recipes.get(choiceIndex) print(self.recipe,"Great selection") Product.make(self.recipe[0], self.recipe[1], self.recipe[2]) else: print("That selection does not exist") selectedProduct = Selector() selectedProduct.select(1)