简体   繁体   English

如何在方法 - 字典中返回给定键的值

[英]How to return the value of given key in method - dictionary

Im trying to write a simple vending machine. 我想写一个简单的自动售货机。 I have Container class that contains items and class Items contains information like the prize and the amount. 我有Container类,其中包含项目和类项目包含奖品和金额等信息。 The ID indentifies the item. ID标识该项目。 Every calling add item will increment ID by one, so that every item is unique. 每个调用添加项都会将ID递增1,因此每个项都是唯一的。 I would like to get the prize of given ID. 我想获得给定身份证的奖品。 So for example: I add item, it has ID=30, I give ID and it returns the prize of it. 所以例如:我添加项目,它有ID = 30,我给ID,它返回它的奖品。

I tried something like this, but it does not work: 我试过这样的东西,但它不起作用:

from Item import Item

class Container:
    id = 30

    def __init__(self, objects=None):
        if objects is None:
            objects = {}
        self.objects = objects

    def add_object(self, obj: Item):
        self.objects.update({id: obj})
        Container.id = container.id + 1

    def get_length(self):
        return len(self.objects)

    def find_price_of_given_id(self, id):
        # return self.objects.get(id).get_price()
        pass


Cola = Item(20)
print(Cola.get_amount())
container = Container()
container.add_object(Cola)
print(container.objects.items())

Item class: 物品类:

class Item:
    def __init__(self, price,amount=5):
        self.amount = amount
        self.price = price

    def get_price(self):
        return self.price

    def get_amount(self):
        return self.amount

I dont know why also print(container.objects.items()) returns dict_items([(<built-in function id>, <Item.Item object at 0x00000000022C8358>)]) , why not ID = 30 + Item object 我不知道为什么print(container.objects.items())返回dict_items([(<built-in function id>, <Item.Item object at 0x00000000022C8358>)]) ,为什么不ID = 30 + Item对象

  1. id is the name of a builtin method. id是内置方法的名称。 Don't use it as a variable name - leads to name confusion. 不要将它用作变量名称 - 导致名称混淆。

  2. You're assigning the id inside the container class but never giving it back, so that people can look up the item using the id. 您在容器类中分配了id,但从未将其返回,以便人们可以使用id查找项目。

  3. In python3, dict.items returns a dict_items iterator, so you need to iterate over it to get to the items within. 在python3中, dict.items返回一个dict_items迭代器,因此你需要迭代它以获取其中的项目。

class Item:
    def __init__(self, price, amount=5):
        self.amount = amount
        self.price = price

    def get_price(self):
        return self.price

    def get_amount(self):
        return self.amount

    def __str__(self):
        return f"{self.amount} @ {self.price}"


class Container:
    item_id = 30

    def __init__(self, objects=None):
        if objects is None:
            objects = {}
        self.objects = objects

    def add_object(self, obj: Item):
        id_to_assign = Container.item_id
        self.objects.update({id_to_assign: obj})
        Container.item_id = Container.item_id + 1
        return id_to_assign

    def get_length(self):
        return len(self.objects)

    def find_price_of_given_id(self, item_id):
        return self.objects.get(item_id).get_price()


Cola = Item(20)
print(Cola.get_amount())
container = Container()
cola_id = container.add_object(Cola)
print(container.objects.items())
print(container.find_price_of_given_id(cola_id))

Output: 输出:

5
dict_items([(30, <__main__.Item object at 0x104444b00>)])
20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM