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__
。我沒有理解爲什麼這種方式可以「緩解某些引導問題」。
有什麼想法?謝謝