[Tutor] Stuck with classes

Kirby Urner urnerk@qwest.net
Thu, 04 Oct 2001 11:08:38 -0700


At 07:41 PM 10/4/2001 +0200, Danny Kohn wrote:
>Hi.
>Have the following problem that I cannot seem to figure out.
>
>class matrixPanel(wxPanel):
>         def __init__(self, parent, id):
>                 wxPanel.__init__(self, parent, id, wxDefaultPosition, 
> wxPyDefaultSize)
>
>         def makeGrid():
>                 self.GridSizer = wxFlexGridSizer( 2, 3, 1, 1 )
>                 [...]

makeGrid should have a self parameter.  wxFlexGridSizer is
global to the module?


>class genGrid:
>         def __init__(self):
>                 pass
>         def addGen(self, panel):
>                 # Create Function table
>                 self.DescWin = wxGrid(...)
>                 [...]
>                 yyy.AddWindow( ...)
>
>p = matrixPanel(frame1, -1)
>gr = p.makeGrid


gr = p.makeGrid makes gr a pointer to the function, doesn't
trigger the function or cause it to return anything.
Use gr = p.makeGrid() if you expect a return value or
want addGen to actually do something (your abbreviated
code doesn't say what this might be).


>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. I have tried that also
>with disappointing results.

p is your matrixPanel object (note:  customary to
capitalize first letter of classes, but not required
of course).

self.GridSizer is a property of matrixPanel objects,
so p.GridSizer will be the specific value of this
property for p.  If you want to pass that in for use
by gg (a genGrid object), just go:

    gg = genGrid(p.GridSizer)

and in genGrid, define __init__ so that it keeps a
copy of p.GridSizer as a member property, e.g.

>class genGrid:
>         def __init__(self,gridsizer):
>                 self.gridsizer = gridsizer

Or you could even just go:

    gg = genGrid()
    gg.gridsizer = p.GridSizer

Example:

   >>> class C:
           def __init__(self):
              pass
           def doit(self):
              print self.message


   >>> o = C()
   >>> o.message = "Hello"
   >>> o.doit()
   Hello

Then, in your addGen, you can reference this value
as self.gridsizer, including when passing parameters
to yyy (but yyy is not defined in this class, so I
don't claim to understand your larger framework, or
what it is you're really doing here).

Kirby