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 <module>
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 <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(list)
that you areappend
ingTableEntry
s to? Default dict expects a zero parameter function for creating it's default value, so you can't actually dodefaultdict(TableEntry)
. – Duhameldefaultdict(list)
give me a dict with { string, [list] }? Whereas I am looking for a dict with { string, TableEntry }. I need to map a String to an object than can hold multiple pieces of information and a string, list dict will not suffice. If I cannot dodefaultdict(TableEntry)
how else could I accomplish this? – Malviadefaultdist(list)
will give you a a { string: [] } dictionary but as you are appending aTableEntry
s you needed a collection, e.g. a dictionary of { string: [TableEntry, TableEntry, ...]}. However, if you just want a dictionary of{string: TableEntry}
why do you need adefaultdict
, wouldn'ttableDict = {}; tableDict[package] = TableEntry(...)
not work? – Duhamel