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.
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.
First off my app can be initialized as part of a twistd plugin, or as a standalone script.
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
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.
----- Edit ------
To clarify a couple points, the script is called with python app.py
app.py references a module called resources.py
which is a bunch of class definitions. In some of the classes, when executed, they reference the 'singleton' instance of app.myapp.