REFERENCE TO FUNCTION AS THE ARGUMENT IN CLASS CONSTRUCTOR

#Hello, i will be extremely thankful if someone help me with solving the problem:
#i would like to pass on argument which is function reference, in other words
#i need to use the function as a argument and execute it

from wxPython.wx import *

# --------------------------------------------------------

class main_window(wxFrame):
  def __init__(self, parent, id, title):
    wxFrame.__init__(self, parent, -1, title, size=(800,600), style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE|wxWS_EX_VALIDATE_RECURSIVELY)

    #panel = NEW(self,-1, ANYTHING)=> PASSING ON ANYTHING FUNCIONT
    
    self.Show(true)

  def ANYTHING(self):
    print 'anything'
# --------------------------------------------------------

class NEW(wxPanel):
  def __init__(self, parent,ID, func):
    wxPanel.__init__(self, parent, ID,pos = (10,10),size = (100,100))

    EVTEVT_LEFT_DOWN(self, self.OnLeftDown)
  def OnLeftDown(self,evt):
    pass
    #>>>> HERE I WANT TO EXECUTE FUNCTION "FUNC"

class App(wxApp):
  def OnInit(self):
    frame = main_window(None, -1, "WINDOW")
    self.SetTopWindow(frame)
    return true

# --------------------------------------------------------

app=App(0)
app.MainLoop()

···

##
## THANKS IN ADV.
##
## Przemek Drochomirecki (Jagiellonian University)

Przemo wrote:

#Hello, i will be extremely thankful if someone help me with solving the problem:
#i would like to pass on argument which is function reference, in other words
#i need to use the function as a argument and execute it

from wxPython.wx import *

class main_window(wxFrame):
    def __init__(self, parent, id, title):
        wxFrame.__init__(...) panel = NEW(self,-1, ANYTHING)=> PASSING ON ANYTHING FUNCIONT
        self.Show(true)

    def ANYTHING(self):
        print 'anything'
# --------------------------------------------------------

class NEW(wxPanel):
    def __init__(self, parent,ID, func):
        wxPanel.__init__(self, parent, ID,pos = (10,10),size = (100,100))

        EVTEVT_LEFT_DOWN(self, self.OnLeftDown)
    def OnLeftDown(self,evt):
        pass
        #>>>> HERE I WANT TO EXECUTE FUNCTION "FUNC"

You're very close, actually. The first problem that you have is that you're not *quite* naming the function right when you're passing it in to the panel. You're passing the bare name 'ANYTHING', but the function (method really) in question is part of a class -- indeed, part of the same class as the __init__() method that you're in. There's a couple options here -- you can pass in the method as it is by naming it self.ANYTHING, i.e.

     panel = NEW(self, -1, self.ANYTHING)

Or, you can change ANYTHING() so that it is, indeed, an independent function. That would involve removing a level of indentation (so that the def lines up with the class statements), and removing the 'self' from its argument list. (It may also involve moving the definition so that it's not in the middle of one of your class definitions.)

Once you've done that, the function object will get passed to your NEW panel, and will be given the name func. You'll need to save a reference to it, so that you can access it in other event handlers. You then use it just like you would any other function.

class NEW(wxPanel):
     def __init__(self, parent,ID, func):
         wxPanel.__init__( ... )
         # you can call func here as normal, i.e. func()
         # or you can save a reference for later...
         self.function = func
         EVT_LEFT_DOWN(self, self.OnLeftDown)

     def OnLeftDown(self,evt):
         # here you can use the previously saved reference
         self.function()

Hope that helps!

Jeff Shannon
Technician/Programmer
Credit International