python定义字典对象时_Python-使用defaultdict制作自定义对象的字典

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

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)

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

printMetaData(lines, f)

File "wiki.py", line 73, in printMetaData

tableDict[package].append(entry)

TypeError: __init__() takes exactly 5 arguments (1 given)

I have also tried the following:

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

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)

解决方案

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)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值