Python best practice instantiating classes in app
dboland9 at offilive.com
Mon Apr 29 13:38:41 EDT 2019
As apps get more complex we add modules, or Python files, to organize
things. One problem I have is a couple of data classes (list of
dictionary objects) in a few modules that are used in a number of the
other modules. For example a list of meter reading dictionaries in one
module is used by the user interface module to get the data from the
user, a report module to display the data, and a file module to save and
retrieve the data to/from file. All the modules need to use the same
instance of the list classes.
There are a number of ways to do this. One is a module that creates the
objects, then import that module into all of the others. Works well,
but may not be the best way to do the job.
A slight variation is to do this in the main module, but the main module
has to be imported into the others. Since I use the main module as a
calling module to load data, start the user interface, and to close
things down, it may not be the best place to also create the classes.
Another way to do this is have a line in each module to check the name.
If it is the module name, then create the class and then import these
modules in all the others. A tad messy and maybe a little confusing.
So what are the suggestions from people that have been down this road
More information about the Python-list