Python pattern for sharing configuration throughout application

前端 未结 4 998
逝去的感伤
逝去的感伤 2020-12-14 07:36

I have an application consisting of a base app that brings in several modules. The base app reads a parameter file into a configuration hash, and I want to share it across a

4条回答
  •  借酒劲吻你
    2020-12-14 08:34

    You could just:

    import config
    

    and have a global config module


    excerpts from my comments:

    You can always add special rules for odd situations by just saying oddValue if isOddSituation() else config.normalValue.

    If you want to have configuration modules be hierarchically subclassable (like my other answer describes), then you can represent a config as a class, or you can use the copy module and make a shallow copy and modify it, or you can use a "config dictionary", e.g.:

    import config as baseConfig
    config = dict(baseConfig, overriddenValue=etc)
    

    It doesn't really matter too much which scope you're in.

提交回复
热议问题