Python-使用defaultdict制作自定义对象的字典 [英] Python - Using defaultdict to make dictionary of custom objects

查看:164
本文介绍了Python-使用defaultdict制作自定义对象的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下课程。包,网站和注释都是字符串,而distroDict是(字符串,列表)字典。

I have created the following class. Package, website and comments are all strings and distroDict is a (string, list) dictionary.

class TableEntry(object):

    def __init__(self, package, website, distroDict, comments):
        self.package = package
        self.website = website
        self.distroDict = distroDict
        self.comments = comments

我想使用defaultdict(TableEntry)制作一个(string, TableEntry)自定义词典。

I want to use defaultdict(TableEntry) to make a (string, TableEntry) custom dictionary.

tableDict = defaultdict(TableEntry)
entry = TableEntry(package, website, distroDict, comments)
tableDict[package].append(entry)

我希望软件包成为键,并将输入对象作为值。我可以在入口对象中使用值,但是如果尝试将其附加到tableDict上,则会收到以下错误。

I want the package to be the key and the entry object to be the value. I can use values within the entry object but if I try to append it to tableDict I receive the following error.

Traceback (most recent call last):
  File "wiki.py", line 151, in <module>
    printMetaData(lines, f)
  File "wiki.py", line 73, in printMetaData
    tableDict[package].append(entry)
TypeError: __init__() takes exactly 5 arguments (1 given)

我也尝试了以下方法:

tableDict[package].append(TableEntry(package, website, distroDict, comments))

并从本质上收到相同的错误:

And receive the same error essentially:

Traceback (most recent call last):
  File "wiki.py", line 150, in <module>
    printMetaData(lines, f)
  File "wiki.py", line 73, in printMetaData
    tableDict[package].append(TableEntry(package, website, distroDict, comments))
TypeError: __init__() takes exactly 5 arguments (1 given)


推荐答案

当您尝试从defaultdict获取信息时,它将返回您实例化的 default_factory ,在您的情况下是没有方法的 TableEntry 追加。但这实际上不是为什么您会得到此错误。发生这种情况是因为返回实例时也会对其进行评估,并且由于 TableEntry 没有所需的参数,因此Python解释器会抱怨。但是,即使在 defaultdict 调用中为 TableEntry 提供了参数,您也可能会收到 TypeError ,因为 defaultdict 需要使用可调用对象(而不是类的实例)进行调用。

When you try to get something from a defaultdict it returns an instance of the default_factory you instantiated it with, which in your case is TableEntry that has no method append. But that's actually not why you are getting the error. This happens because when an instance is returned it is also evaluated, and since TableEntry doesn't have the arguments it needs, the Python interpreter will complain. But even if TableEntry was given arguments in the defaultdict call, you would probably get a TypeError because defaultdict needs to be called with a callable (and not an instance of a class, as it would be).

总而言之,您无法使用不可调用的,除非您使用子类化。相反,您应该执行以下操作:

In summary, you cant instantiate a defaultdict with an non-callable unless you use subclassing. Instead you should do something like this:

class TableEntry(object):
    def add(self, package, website, distroDict, comments):
        self.package = package
        self.website = website
        self.distroDict = distroDict
        self.comments = comments

tableDict = defaultdict(TableEntry)
entry = ("package", "website", "distroDict", "comments")
tableDict["package"].add(*entry)

然后您可以执行例如 tableDict [ package] [ website] 来获取您写入 self.website 的字符串。

Then you can do e.g. tableDict["package"]["website"] to get the string you wrote to self.website.

这篇关于Python-使用defaultdict制作自定义对象的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆