Python best practice instantiating classes in app
Terry Reedy
tjreedy at udel.edu
Mon Apr 29 15:26:18 EDT 2019
On 4/29/2019 1:38 PM, Dave wrote:
> 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,
You can have one *or more* such modules. Perhaps you already do.
> but may not be the best way to do the job.
In what way do you consider it unsatisfactory.
>
> A slight variation is to do this in the main module, but the main module
> has to be imported into the others.
Avoid import loops unless really necessary. They often work, but when
they don't ... its a pain.
> 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.
Right.
--
Terry Jan Reedy
More information about the Python-list
mailing list