call function of class instance with no assigned name?

Carl Banks pavlovevidence at gmail.com
Thu May 7 00:07:42 CEST 2009


On May 5, 12:17 pm, George Oliver <georgeolive... at gmail.com> wrote:
> A handler would be something like a key input handler. The key input
> handler is defined separately from the command handler in the case I
> want to use a different method of input, for example a mouse or
> joystick.
>
> In the dictionary of key inputs I could map each input directly to a
> function. However there is no instance name I can call the function
> on, as I create a thing, add a brain, and add handlers to the brain
> like this:

What instance do you want to call the functions on?  This is the part
that confuses me.


> player = Thing(26, 16, 'player')
> player.brain = Brain()
> player.brain.add_handlers(
>                             commandHandler(player.brain, player),
>                             keyboardHandler(player.brain),
>                             fovHandler(player.brain))
>
> So what I'm wondering is how to reference the instance in each brain's
> list of handlers when I want to map something like a key input to a
> command, or what a better way might be to structure the code.


I'm going to guess that you want the keyboardHandler to call method of
commandHandler.  There's no reason for commandHandler to be a handler
at all then: keyboardHandler is already handling it.

So here is how I would change it:

player = Thing(26,16,'player')
player.brain = Brain()
responder = commandResponder(player.brain,player)
player.brain.add_handlers(
    keyboardHandler(player.brain,responder),
    joystickHandler(player.brain,responder),
    fovHandler(player.brain),
    )


Carl Banks



More information about the Python-list mailing list