【发布时间】:2012-05-21 23:28:39
【问题描述】:
我正在尝试在 python 中围绕字典对象编写一个包装器对象,就像这样
class ScoredList():
def __init__(self,dct={}):
self.dct = dct
list = ScoredList()
list.dct.update({1,2})
list2 = ScoredList()
list.dct.update({"hello","world"})
print list1.dct, list2.dct # they are the same but should not be!
似乎我无法创建新的 ScoredList 对象,或者更确切地说,每个评分列表对象都共享相同的基础字典。这是为什么呢?
class ScoredList2():
def __init__(self):
self.dct = {}
ScoredList2 的上述代码运行良好。但我想知道如何在 python 中正确地重载构造函数。
【问题讨论】:
-
{"hello","world"}不是字典,{1,2}也不是。
标签: python dictionary overloading