Data access from multiple code modules

Jeremy Jones jemejones at gmail.com
Wed Jul 12 10:30:26 EDT 2006


simon.hibbs at gmail.com wrote:
> Lets say that I have an application consisting of 3 files. A main.py
> file, gui.py and a data.py which handles persistent data storage.
> Suppose data.py defines a class 'MyDB' which reads in data from a
> database, and main.py creates an instance of this object. How does code
> in gui.py access this object? Here's simplified pseudocode:
>
> MAIN.PY
> import gui, data
> DataObject = data.MyDB(blah)
>
> How do I write code in gui.py that can access DataObject? Is this
> entirely the wrong way to approach this sort of problem?
>
> Actualy the problem is more complex because the GUI consists of a main
> GUI form, and panels defined as seperate objects in seperate files.
> Various panels will contain controlls for manipulating data in the
> DataObject, or wherever data storage end up.

What does main.py do?  Are you creating an instance of the gui thingy?
If so, you could just pass DataObject into your gui thingy either into
the constructor or to a setter once you create an instance of it.  If
the gui needs any database stuff at instantiation time, you probably
need to pass it into the constructor.  However, a main.py, gui.py, and
db.py smells a little like your standard MVC, in which case, you would
get your controller to pass in the data pieces as the GUI needs them.

- Jeremy M. Jones




More information about the Python-list mailing list