Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to initialize a python object once and share across other modules?
    primarykey
    data
    text
    <p>For the life of me I am finding me python transition to be extremely frustrating. One of the things I am attempting at doing is to initialize a single instance of a class from a configuration dictionary, then access that class in other modules.</p> <p>The problems I am facing / the approaches I have taken are not working out and am hoping someone could steer in the right 'pythonic' approach.</p> <p>First off my app can be initialized as part of a twistd plugin, or as a standalone script.</p> <pre><code>import resource class App(object): _config = None _someotherobject = None def __init__(self, config): self._config = config .... def main(): global myapp myapp = App({}) # Could use help here, how to pass config to it myapp = None #Global doesnt work as I expect, it doesnt modify this instance, stays as None if __name__ == '__main__': main() #-----------resource.py class Foo(object): def foo(self): app.myapp.somefunction() #NoneType object has no attribute </code></pre> <p>I have verified the app object is created before the code in the other module is kicked off. I just can't figure out why the 'global' instance in the module doesn't actually do what I expect, also confused as to how to reference the instance from another module.</p> <p>----- Edit ------ To clarify a couple points, the script is called with <code>python app.py</code> app.py references a module called <code>resources.py</code> which is a bunch of class definitions. In some of the classes, when executed, they reference the 'singleton' instance of app.myapp.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload