inheritance - In python how do I access an instance of a class from a parent or top-level module from withing a submodule -


i'm python newbie, , developing first python app using cherrypy web server , jinja 2 templating engine.

i'm using velleman k8055 usb experiment board, has python module i'm importing.

for k8055 function have create instance of class within k8055 module, open connection board... far understand have keep connection/instance running, , use sole instance control board, within sub-modules.

i'm having hard time figuring out how access initialised instance of k8055 within sub-modules/packages...

i have application structure similar following...

smartypi/ smartypi/smartypi.py smartypi/smartypi/__init__.py smartypi/smartypi/scheduler/ smartypi/smartypi/scheduler/__init__.py smartypi/smartypi/scheduler/scheduler.py smartypi/smartypi/web/ smartypi/smartypi/web/__init__.py smartypi/smartypi/web/k8055.py smartypi/smartypi/web/root.py smartypi/smartypi/web/schedule.py 

in 'smartypi/smartypi.py' have initialised instance of k8055 object with:

from pyk8055 import k8055 k = k8055(0) 

i'd able access same instance of 'k' places like:

smartypi/smartypi/web/k8055.py smartypi/smartypi/web/schedule.py 

i can't seem figure out on own...

was 'smartypi/smartypi.py' not best place create k8055 instance?

should making somewhere else, , how can make work need to?

add k = k8055(0) pyk8055 module (at end). in other modules replace:

from pyk8055 import k8055 

with

from pyk8055 import k 

note: suggest rename k more unique.

in case cannot edit puk8055 module, create own, call i.e. globals.py:

from pyk8055 import k8055 k = k8055(0) 

after in other modules instead directly importing pyk8055, import module globals.py:

from globals import k 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -