I’m currently learning Flask and I just set up a config file I load into the app with:
app.config.from_object('myconfigmodule')
The config module has two classes in it, Config and DebugConfig and DebugConfig inherits Config. I’d like to use @property getters to get config variables rather than accessing them with app.config['myvar'] because it makes for cleaner code. I set this up and app.config does not see the properties but I can still access the config class members with app.config['myvar']
This is the error I get when I start my app:
Traceback (most recent call last):
File "runserver.py", line 3, in <module>
app.run(host=app.config['_APP_HOST'], debug=app.config.Debug)
AttributeError: 'Config' object has no attribute 'Debug'
In the config class the Debug property is as follows:
class Config (object):
_APP_DEBUG = False
@property
def Debug (self):
return self._APP_DEBUG
Am I doing something wrong here or does Flask just not like properties in configs for some reason? Thanks for any help!
Flask has it’s own
Configclass (a dict subclass) and it will pick out the attributes of the object given tofrom_object, rather that using the given object as is, as can be seen in the source code:As you can see, it will only use uppercase attributes.
Here’s an example by hand:
That said, nothing will hold you back, if you want to implement something like a dot-dict’d subclass of flasks’
Config. Whether the potential confusion caused by a non-standard approach outweighs the gains in code readability is something you can decide based on the scope of your project.