By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,441 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,441 IT Pros & Developers. It's quick & easy.

Import config into global space?

P: n/a
I'm writing a module, and it's getting too big to cope with as it has
a lot of configuration in it - constants, dictionaries etc; it'd be
nice to externalise this and keep my module down to just classes/defs
etc.

What I'd like to do is have myprog.py import config.py and be able to
reference the configuration variables globally - i.e. I could just
reference myvar instead of config.myvar

So I'd have something like this:

config.py
=========

ID_TOOLBAR = 105

myprog.py
=========

import config
print ID_TOOLBAR

result
======
105


It doesn't seem to work though, as global is limited to modules as far
as I know.

Any ideas how to do this, it's basically like a require or #include in
Perl/C...
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> config.py
=========

ID_TOOLBAR = 105

myprog.py
=========

import config
print ID_TOOLBAR

result
======


from config import *

- Josiah
Jul 18 '05 #2

P: n/a
Josiah Carlson <jc******@uci.edu> wrote:

[global import from file]
from config import *


Oh man! I can't believe it was that simple and I didn't think of it!

Actually, I've gone with the config.variable syntax for now anyway, it
actually makes more sense as I don't have to name my variables like
fontsize_config to differentiate them.
Jul 18 '05 #3

P: n/a
simo wrote:
Josiah Carlson <jc******@uci.edu> wrote:

[global import from file]

from config import *

Oh man! I can't believe it was that simple and I didn't think of it!

Actually, I've gone with the config.variable syntax for now anyway, it
actually makes more sense as I don't have to name my variables like
fontsize_config to differentiate them.

In general, "from module import *" is generally frowned upon, if only
because it can clobber your module's namespace. There are certainly
exceptions, but namespaces are useful.

- Josiah
Jul 18 '05 #4

P: n/a
Josiah Carlson <jc******@uci.edu> wrote:

[snip]
from config import *
Actually, I've gone with the config.variable syntax for now anyway, it
actually makes more sense as I don't have to name my variables like
fontsize_config to differentiate them.
In general, "from module import *" is generally frowned upon, if only
because it can clobber your module's namespace. There are certainly
exceptions, but namespaces are useful.


Yes, that's what I thought, one of the reasons I'm going to go with
the config.variable syntax after all.

I'm moving to the wxWidgets 2.5 way of using the wx namespace too
(e.g. wx.ListCtrl instead of wxListCtrl).
Jul 18 '05 #5

P: n/a
> Yes, that's what I thought, one of the reasons I'm going to go with
the config.variable syntax after all.

I'm moving to the wxWidgets 2.5 way of using the wx namespace too
(e.g. wx.ListCtrl instead of wxListCtrl).


That's good, because wxListCtrl won't be available in 2.5, wx.ListCtrl
will be. *wink*

- Josiah
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.