【发布时间】:2014-07-27 06:05:55
【问题描述】:
import unittest
from UserDict import UserDict
class MyDict(UserDict):
def __init__(self, x):
UserDict.__init__(self, x=x)
class Test(unittest.TestCase):
def test_dict(self):
m = MyDict(42)
assert {'x': 42} == m # this passes
self.assertDictEqual({'x': 42}, m) # failure at here
if __name__ == '__main__':
unittest.main()
我明白了
AssertionError: Second argument is not a dictionary
我应该使用内置的dict 作为基类,而不是UserDict?
【问题讨论】:
-
可能吗?
UserDict是什么?它来自哪里(和why does the module have capital letters in its name?)? -
@HenryKeiter 见docs.python.org/2/library/userdict.html
-
您始终可以将
m转换为dict:dict(m)或使用m.data而不是m。 -
@alecxe Eww... 从来不知道。我猜提问者应该测试
m.data? -
首先,考虑一下你是否真的需要自己的 dict 类,这种情况很少见。其次,只是继承自
dict,我不知道有什么理由要继承自UserDict。 (在非常旧的 python 版本中这是必需的)。