
defaultdict文档说
default_factory:
如果default_factory属性为None,则使用密钥作为参数会引发KeyError异常。
如果仅将defaultdict的default_factory设置为
None怎么办?例如,
>>> d = defaultdict(int)>>> d['a'] += 1>>> ddefaultdict(<type 'int'>, {'a': 1})>>> d.default_factory = None>>> d['b'] += 2Traceback (most recent call last): File "<stdin>", line 1, in <module>KeyError: 'b'>>>不知道这是否是最好的方法,但似乎可行。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)