[Tutor] How to import a dictionary from another module?

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Jun 15 17:07:34 CEST 2015


On 15/06/2015 12:03, David Aldrich wrote:
> Hi
>
> I have defined a dictionary in a module:
>
> mydir.py:
>
> REG_LOOKUP = {
>      'REG_1' : some_value1,
>      'REG_2' : some_value2,
> }
>
> How would I import that dictionary from my main() function (which lives in a different module) please?
>
> Best regards
>
> David
>

import othermodule
...
othermodule.REG_LOOKUP{whatever}

OR

from othermodule import REG_LOOKUP
...
REG_LOOKUP{whatever}

You can also use

from othermodule import *

but this is frowned upon as it pollutes your namespace, potentially 
giving all sorts of weird and wonderful situations.  I never use it.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list