Skip to content Skip to sidebar Skip to footer

Accessing Class Instance From Another Module (python)

I'm pretty new to Python as to OOP in general which is probably be the reason that I can't figure out the following: I'm writing a python script which opens a text file and subsequ

Solution 1:

I might be misinterpreting your question, correct me if I am. As I understand it, you are using one single instance of a SetEnv object across an entire project to store and modify some path configuration.

If you really want a singleton like settings object, then use a module instead of a class.

# env.py
_src = ''
_html = ''defset_path_srcfile(path_srcfile):
    global _src
    _src = path_srcfile

defget_path_srcfile():
    return _src

...

Then everywhere you need it you can use import env; env.set_path_srcfile(myfile) and know that all other functions / modules / classes will be aware of the update.

If you don't want a singleton, then making a settings object available in the main module somewhere (as you have done) is a fine solution.

Post a Comment for "Accessing Class Instance From Another Module (python)"