Data access from multiple code modules

Bruno Desthuilliers onurb at xiludom.gro
Wed Jul 12 12:16:05 EDT 2006


simon.hibbs at gmail.com wrote:
> Jeremy Jones wrote:
> 
> 
>>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.
> 
> 
> It's a wxPython app. I created the GUI initialy using wxGlade which
> gave me a small myapp.py script, a file containing the main application
> frame (containing a listbook controll) and several files containing
> panel classes. Each panel class contains controlls for performing
> various operations on the data set (adding records, deleting them,
> running various transformations).

Do you mean the code effectively doing these operations is in the gui ?
If yes, it would be better to factor it out IMHO.

> I can't say I understand how it all
> works at a deep level,
> although I've been hacking it about quite
> successfuly so far.
> 
> Presumably if I pass DataObject through to the Frame object, and from
> there through to the Panel objects then presumably this will solve the
> probelm?

Presumably !-)

> I guess it would be passed by reference so all the panels
> would be working on the same data object?

Python doesn't really have a "pass by value" semantic, since "variables"
are really just names bound to (ie : refering to) objects. So when it
comes to arguments passing, the argument *name* is local to the function
(rebinding the name won't affect the binding in the caller's namespace),
but what's bound to the name is really a reference to the object (so
mutating the object will effectively affect it).

> Doh! How simple. Why didn't I think of that? I'm too used to procedural
> scripts where you'd just put everything in a global data structure. I
> know this is bad,

Well, depends on the size of the script. But it sure doesn't scale !-)

And FWIW, even with procedural, it's possible (and usually better) to
pass arguments around instead of having a big global datastructure.
Classes are handy when many functions needs to share state (work on a
same dataset), but there's no way to have one call the other and pass it
the dataset.

> but it's hard to get out of that mentality.

Seems you're on the right way !-)

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list