[wxPython-users] Learning the MVP Pattern

Christopher Barker Chris.Barker at noaa.gov
Wed Nov 22 11:38:43 PST 2006


Rich Shepard wrote:

>   I have two auxiliary modules, each containing one or more classes, and
> similar modules in each class.

huh? or are you using "module" in two different ways:

1) python module
2) chunk of similar code

> What I need to learn are 1) how to call them
> from the base modules and 2) how to define the methods and functions in the
> auxiliary modules.

I think the key to remember is that there is NO global namespace in 
python. Each module has its own namespace, each class is a namespace, 
each instance is a namespace, each function is it's won namespace.

not quite true:

import ModuleName

Will import the same module everywhere, but that's not quite the same.

There is a lot of variation of where you put any piece of code, but a 
few rules of thumb may help:

DRY: Don't repeat yourself -- you know this already

When you implement patterns like MVP, separating gui from logic, or 
indeed, and logical breaking up of code, you will have to write some 
extra code. Things like:

SaveButton.Bind(EVT_BUTTON, self.OnSaveButton)
...
def OnSaveButtonClick(self, event):
     self.Model.SaveData()

So here an event handler does nothing but call the right method in 
another class. It seems like a bit of a waste, but it keeps things clear 
and separate. I think you've been trying to do things like:

SaveButton.Bind(EVT_BUTTON, self.Model.SaveData)

You can do that, but by directly calling the Model method, you've 
mingled your GUI and logic, and you may start getting confused about 
name spaces.

Note also that I've got: self.Model

For a GUI object (this could be a panel, frame, dialog, whatever), to be 
able to work with a Logic object, it needs a reference to it -- that 
seems to be getting you hung up in various ways too.

You might like the book:

"The Pragmatic Programmer" by Hunt and Thomas -- there's  lot of god 
stuff in there. They're not talking about Python though (it's very 
language-neutral), so it won't help you with namespace confusion.

All I can say about that is every time you write:

something.AnAttribute

think: What is something? What attributes does it have?

When you create a class, think: what other objects does this class need 
to know about? then decide how you are going to access them -- store 
them in self.class, access them through a singleton, like a module or 
wx.App object, etc.

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the wxpython-users mailing list