You really asked two questions in one.
For the second about storing configuration, you can
write your vars in say, a 'config.py' module. Then import config as cfg
and manipulate using cfg.
qualified names. You can also from config import *
for direct use but that's not recommended and you loose write capability in the original config module. They are copied into current namespace, not linked.
simply use a dictionary to hold your settings like Walter said. They are the obvious builtin type for that.
- make your own Configuration class, instantiate
cfg
and manipulate attributes through cfg.
qualifier.
- use a shelf, a persistant dict with the shelve module.
- Because shelves use pickle and this is a binary format used mostly in Python (I think)to store objects, you may prefer to use some more human oriented open format like suggested by DeepSpace and Cartucho (see configparser for
.ini
and so on).
- Much like with the class case, use a namedtuple or a SimpleNamespace (didn't know this one) like Wouda and luminousmen said.
Probably there are even more choices but I think the list looks enough for most tastes.
Now for the first question, that I think is a very good one, I am almost sure that the answer is no, you can't do that in Python. From here I found this which is the reasoning for self
use obligation. Safety, explicitness, and performance it looks. I suppose if it were to be allowed, you could use it too for self
, and maybe that's why you can't.
I sometimes miss that feature too.