Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did I need to specify a specific class to import in python?

Tags:

python

I just upgraded to Python 2.7.1 (on Mac) so I could use OrderedDicts.

After trying to run the following script:

import collections

test = OrderedDict()

I got:

NameError: name 'OrderedDict' is not defined

I fixed it with:

from collections import OrderedDict

...but I want to know why I needed to do that?

Why didn't the broad import collections work for me?

like image 622
eastydude5 Avatar asked Apr 18 '11 23:04

eastydude5


1 Answers

import collections

imports the collections module into the current namespace, so you could work with this import like this:

import collections
orderedDict = collections.OrderedDict()


from collections import OrderedDict

imports just the specified class into the current namespace.

like image 58
Nicola Coretti Avatar answered Sep 22 '22 19:09

Nicola Coretti