恐怕你在这里的回答会归结为一些相当无聊的东西:Counter 是用 Python 编写的,defaultdict 是用 C 编写的。
这里是collections.py。请注意,您可以向下滚动并找到Counter 的标准类定义:
########################################################################
### Counter
########################################################################
class Counter(dict):
'''Dict subclass for counting hashable items. Sometimes called a bag
or multiset. Elements are stored as dictionary keys and their counts
are stored as dictionary values.
...
'''
但是,defaultdict 是从 _collections 导入的:
from _collections import deque, defaultdict
如this answer 中所述,这是用 C 编写的内置扩展。
如果你对deque(也是C)或collections中的其他一些用Python编写的类进行字符串化,你会注意到你会得到同样的行为:
>>> from collections import deque
>>> str(deque)
"<type 'collections.deque'>"
>>> from collections import OrderedDict
>>> str(OrderedDict)
"<class 'collections.OrderedDict'>"*