2012-08-27 60 views
3

我正在閱讀collections集合模塊的源代碼。這個模塊是兩個文件的組合:collections.py和_abcoll.py。 這是模塊的doc,它包含指向源代碼的鏈接。關於collections.py和_abcoll.py(python 2.7.3)中引導問題的代碼註釋

在collections.py的開頭:

__all__ = ['Counter', 'deque', 'defaultdict', 'namedtuple', 'OrderedDict'] 
# For bootstrapping reasons, the collection ABCs are defined in _abcoll.py. 
# They should however be considered an integral part of collections.py. 
from _abcoll import * 
import _abcoll 
__all__ += _abcoll.__all__ 
... 

我不太明白什麼是實際的 '引導原因',因爲在_abcoll.py:

6 DON'T USE THIS MODULE DIRECTLY! The classes here should be imported 
7 via collections; they are defined here only to alleviate certain 
8 bootstrapping issues. Unit tests are in test_collections. 
9 """ 
10 
11 from abc import ABCMeta, abstractmethod 
12 import sys 
13 
14 __all__ = ["Hashable", "Iterable", "Iterator", 
15   "Sized", "Container", "Callable", 
16   "Set", "MutableSet", 
17   "Mapping", "MutableMapping", 
18   "MappingView", "KeysView", "ItemsView", "ValuesView", 
19   "Sequence", "MutableSequence", 
20   ] 
... 

_abc.__all__包含所有該文件中的類定義以及collections.py中,它從_abcoll中導入*並將_abcoll.__all__附加到它自己的__all__。我沒有理解爲什麼這種方式可以「緩解某些引導問題」。

有什麼想法?謝謝

回答

1

這個問題似乎是在舊版本的os.py,例如, this one

from _abcoll import MutableMapping # Can't use collections (bootstrap) 

顯然,collections間接要求os(可能是在模塊,其中一些酸洗做,也許在別處結束測試),所以會是沒有_abcoll模塊循環依賴。

(請注意,在Python> = 3.3中,有一個單獨的collections.abc模塊。)