[Tutor] Stuck with classes

alan.gauld@bt.com alan.gauld@bt.com
Fri, 5 Oct 2001 13:47:42 +0100


> On Thu, 4 Oct 2001, Danny Kohn wrote:
> > class matrixPanel(wxPanel):
> > 	def makeGrid():
> > 		self.GridSizer = wxFlexGridSizer( 2, 3, 1, 1 )
> > 
> > class genGrid:
> > 	def addGen(self, panel):
> > 		yyy.AddWindow( ...)
> > 
> > p = matrixPanel(frame1, -1)
> > gr = p.makeGrid
> > gg=genGrid()
> > 
> > How in earth do I pass self.GridSizer in matrixPanel 
> > to yyy? Or do I have to do this in a nested class. 

First, I'd ask why you want to? You are passing an object's 
internal member data first to an intermediate object 
which merely passes it through to a third object (possibly 
an internal attribiute of the second object?). This is 
usually a bad thing in OO design terms. However it may be 
necessary, it's hard to tell how your design works from 
the code snippet.

But personally I'd favour passing the entire panel rather 
than its internal attribute - that way we only need the 
yyy object to be tightly coupled to the panel internals.

The yyy object method can then extract the attribute as 
and when its needed.

But I'd still ask *why* you are passing that around so 
much - maybe the attribute is in the wrong class to 
start with? Could yyy be the true master of that object?
Depends where else and how you use it...

Just some thoughts.

Alan G.

> and then slightly tweak genGrid.addGen() to take in another 
> parameter --- not only the panel, but a GridSizer instance:

But if it has the panel it has the GridSizer too - we're 
just increasing the size of the coupling for no benefit.

> I have to admit, though, that I'm not familiar enough 
> with wxPython to know if this is an wxPythonic approach 

Likewise...

> Alternatively, we can pass the whole matrixPanel off to the 
> genGrid.  

Yes, that's my preferred route assuming the GenGrid really 
is an essential part of the panel to start with.

Alan g.