从字典向类传递参数

2024-09-30 18:25:50 发布

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

我正在尝试将字典选择传递到类函数中,以便进一步处理。但是当我尝试时,我得到一个错误,说我缺少一个参数。我知道我不需要包含self,所以我不明白为什么它不起作用。它应该做到:

  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()

Tags: nameinselfmakedefrecipeproductprice
2条回答

首先,product类中的make方法应该是静态的,这样就可以用类名调用它。即使不使其成为静态的,也可以将第一个参数传递给make function作为self,然后传递其余的参数,但这没有任何意义,因为您使用类名调用make方法。你知道吗

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)

上面的代码生成以下输出

['black', '35', 'cup coffee water'] Great selection cup coffee water dispensing c dispensing u dispensing p dispensing dispensing c dispensing o dispensing f dispensing f dispensing e dispensing e dispensing dispensing w dispensing a dispensing t dispensing e dispensing r

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

相关问题 更多 >