[Tutor] Learning about callbaks

Michael Bernhard Arp Sørensen michaelarpsorensen at stevnstrup.dk
Tue Jan 1 13:54:08 CET 2008

Greetings, my masters.

This is somewhat difficult to transfer to my program with 2 classes/objects.
All examples I've seen is not for more than one instance of a single object.
I use more than one class in my program.

I have a game class and a menu class. When the user chooses "quit" in the
menu, I want the menu object to call a method that executes a quit_program()
from the game class. Obviously, menu is an object within the game object.

class UserInput(CommonBase):
    def __init__(self, queue, game):
        self.loop = 1
        self.queue = queue
        self.game = game

    def main(self):
        while True:
            tstr = raw_input("Input string: ")
            print "Input: ", tstr
            if tstr == "q":

    def quitProgram(self, game, quit_callback):
        self.loop = 0
        game.loop = 0

class Game(CommonBase):
    def __init__(self):
        self.loop = 1
        self.queue = Queue.Queue()

    def startUI(self, tid):
        ui = UserInput(self.queue, self)

    def stoploop():
        self.loop = 0

    def main(self):
        thread.start_new_thread(self.startUI, (1,))

        while self.loop:
                data = self.queue.get(block = False)
            except Queue.Empty:

g = Game()

It is so frustrating not to see the light. I feel that I'm close to
understanding the general idea. Allthough I might be wrong on that point.

I'm desperate.

Thanks in advance.

On Dec 29, 2007 7:58 PM, Dave Kuhlman <dkuhlman at rexx.com> wrote:

> Here is a trivial example:
>    def f1(x):
>        print 'f1: %s' % x
>    def f2(x):
>        print 'f2: %s' % x
>    def use_them(funcs):
>        for func in funcs:
>            func('abcd')
>    def test():
>        funcs = [f1, f2]
>        use_them(funcs)
>    test()

Med venlig hilsen/Kind regards

Michael B. Arp Sørensen
Programmør / BOFH
I am /root and if you see me laughing you better have a backup.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080101/8e70a924/attachment.htm 

More information about the Tutor mailing list