Python 2.7: applying str to collections.Counter and collections.defaultdict
这两个计数器和collections.defaultdict收藏。从继承的。那么是什么原因,这是他们之间的差分输出(非相似类"和"型")?
1 2 3 4 | import collections print str(collections.Counter) print str(collections.defaultdict) |
输出:
1 2 | <class 'collections.Counter'> <type 'collections.defaultdict'> |
恐怕你在这里的回答可以归结为一个相当无聊的问题:
这是
1 2 3 4 5 6 7 8 9 10 | ######################################################################## ### 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. ... ''' |
但是,
1 | from _collections import deque, defaultdict |
号
正如这个答案所指出的,这是一个用C编写的内置扩展。
如果用python编写的字符串iffy
1 2 3 4 5 6 | >>> from collections import deque >>> str(deque) "<type 'collections.deque'>" >>> from collections import OrderedDict >>> str(OrderedDict) "<class 'collections.OrderedDict'>"* |