OrderedDict和defaultdi的子类化

2024-04-19 12:28:03 发布

您现在位置:Python中文网/ 问答频道 /正文

Raymond Hettingershowed一种结合集合类的非常酷的方法:

from collections import Counter, OrderedDict
class OrderedCounter(Counter, OrderedDict):
  pass
# if pickle support is desired, see original post

我想为OrderedDict和defaultdict做类似的事情。当然,defaultdict有一个不同的__init__签名,因此需要额外的工作。解决这个问题最干净的方法是什么?我使用Python3.3。

我在这里找到了一个很好的解决方案:https://stackoverflow.com/a/4127426/336527,但我在想,从defaultdict派生可能会使这个过程更简单?


Tags: 方法fromimportsupportifcounterpasscollections