[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] A question about callback functions



mike@xxxxxxxxx wrote:

Have a look at this interactive session!



...

>>> myclass = eval(classname)
>>> instance = apply(myclass,(3,))
>>> instance.show()
SecondClass 3


You may also want to look at the __import__ built-in.

Oh, regarding the original query, a closer mapping of callback functions than using OO-style instance methods is to use first-class function objects.

   def callback( x, y, z ):
       """Do something cool"""
   registerCallback( callback )

You can use a first-class function object stored in a variable just as you would by referencing it by name. It's basically a better way of doing "function pointers" :) .

Lastly, if you want to be even more flexible, you can use something like PyDispatcher and send events from your objects which the client's code can observe. That allows for multiple levels of customisation all registering for the same callback "event", for instance.

Have fun,
Mike

--
________________________________________________
 Mike C. Fletcher
 Designer, VR Plumber, Coder
 http://www.vrplumber.com
 http://blog.vrplumber.com