Modules in Python are singleton objects, and using one to store the settings used by the other modules would be a very Pythonic
The second line of the "nasty thing" is just setting the attributes of a module named settings
and so isn't that bad. What's worse is the _get_kwargs()
part of the first line which is accessing a private attribute of the argparse.Namespace
object returned by parser.parse_args()
to get the names and values of the settings parsed from the command-line. A slightly better way to do it might be something like this:
import settings # possibly empty .py file
for name, val in vars(parser.parse_args(sys.argv[1:])).iteritems():
setattr(settings, name, val)
However this won't fix your IDE problems because the IDE doesn't know the name of settings added dynamically. A simple way to fix that would be to define all the possible attributes with some kind of default values in a settings.py
module instead of having an empty one.
The first time a module is import
ed an entry for it is added to the sys.modules
dictionary with its name as the key and an instance of types.ModuleType
as a value. Subsequent import
s will first check to see if an entry for it already exists and will skip reloading the file if it does -- which is why I claim they're basically singleton objects. Modifications made to its attributes will immediately be visible to other modules that have imported it or do so afterwards, so it's generally a good data sharing mechanism within an application.