简体   繁体   English

python类中的全局字典

[英]global dictionary within a class python

I'm designing an inventory class in Python, it is supposed to keep track of the items a store has in stock, add new ones, and delete them, as well. 我正在用Python设计一个库存类,它应该跟踪商店中库存的物品,添加新物品以及删除它们。

The trouble comes from my "item" definitions within the class. 麻烦来自于我在课堂上的“项目”定义。 When I add another item to my dictionary, it replaces it, it doesn't add it. 当我在字典中添加另一个项目时,它会替换它,但不会添加它。 I appreciate your help! 我感谢您的帮助! Why won't it add??? 为什么不添加???

class Store:

    def __init__(self, name, email):
        self.name = name
        self.email = email

    # two accessor methods
    def getName(self):
        return self.name

    def getEmail(self):
        return self.email

    # makes print work correctly
    def __str__(self):
        return str(self.name)

    # items
    def additem(self, item, price):
        global items
        items = {}
        self.item = str(item)
        self.price = float(price)
        items[self.item] = price

    def delitem(self, item):
        items.remove(item)

    def displayinventory(self):
        return items

You are setting items to a new empty dictionary every time you call additem . 每次调用additem时,您都将items设置为新的空字典。 So it always erases whatever's there before adding a new item. 因此,在添加新项目之前,它始终会擦除所有内容。 Instead, set items = {} once outside the function. 而是在函数外设置items = {} There is also no point in doing self.item = str(item) (and the same for the price), because this will just overwrite the existing self.item , so you'll only have access to the most recent one. 进行self.item = str(item) (与价格相同self.item = str(item)也没有意义,因为这只会覆盖现有的self.item ,因此您只能访问最新的self.item

Actually, what you probably should do is make items an attribute of the object, like this: 实际上,您可能应该做的是使items成为对象的属性,如下所示:

class Store:

    def __init__(self, name, email):
        self.name = name
        self.email = email
        self.items = {}

    # rest of your code here. . .

    def additem(self, item, price):
        self.items[str(item)] = float(price)

    def delitem(self, item):
        del self.items[str(item)]

    def displayinventory(self):
        return self.items

The way you're doing it, there's only one global items dict that will be shared among all Stores. 您这样做的方式是,只有一个全局items dict将在所有商店之间共享。 The above way gives each store its own items dict so it can keep its own record of its own items. 上面的方法为每个商店提供了自己的商品字典,因此可以保留自己的商品记录。

Even this was asked a view years ago, others might be interested in this answer. 即使这是几年前提出的一个观点,其他人可能对此答案也很感兴趣。 If you want to use a dictionary globally within a class, then you need to define it in section where you use your class. 如果要在类中全局使用字典,则需要在使用类的部分中对其进行定义。 if you are using your class in main, then define it there. 如果您在main中使用您的类,则在其中进行定义。 A dictionary or o list are global by default. 默认情况下,字典或o列表是全局的。

class Store:

    ...

    def additem (self, item, price):
        self.item = str (item)
        self.price = float (price)
        items [self.item] = price

def main ():
    ...
    items = dict ()
    myStore = Store ()
    ....

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

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