Python pattern for sharing configuration throughout application

前端 未结 4 984
逝去的感伤
逝去的感伤 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:37

    Answering old question:

    Just use dependency injection as suggested by @Reed Copsey here. E.g.

    class MyClass:
        def __init__(myConfig):
            self.myConfig = myConfig
            ...
    
        def foo():
            self.myConfig.getConfig(key)
            ...
            self.myConfig.setConfig(key,val)
            ...
    
    ...
    # myConfig is your configuration management Module/Class
    obj = SomeClass(myConfig)
    

提交回复
热议问题