我刚刚升级到Python 2.7.1(在Mac上),所以我可以使用OrderedDicts.
尝试运行以下脚本后:
import collections
test = OrderedDict()
Run Code Online (Sandbox Code Playgroud)
我有:
NameError: name 'OrderedDict' is not defined
Run Code Online (Sandbox Code Playgroud)
我修复它:
from collections import OrderedDict
Run Code Online (Sandbox Code Playgroud)
......但我想知道为什么我需要这样做?
为什么广泛的import collections工作不适合我?
Nic*_*tti 22
import collections
Run Code Online (Sandbox Code Playgroud)
将集合模块导入当前名称空间,因此您可以使用此导入:
import collections
orderedDict = collections.OrderedDict()
from collections import OrderedDict
Run Code Online (Sandbox Code Playgroud)
仅将指定的类导入当前名称空间.