call function of class instance with no assigned name?

George Oliver georgeoliverGO at gmail.com
Tue May 5 17:52:39 CEST 2009


hi, I'm a Python beginner with a basic question. I'm writing a game
where I have keyboard input handling defined in one class, and command
execution defined in another class. The keyboard handler class
contains a dictionary that maps a key to a command string (like 'h':
'left') and the command handler class contains functions that do the
commands (like def do_right(self):),

I create instances of these classes in a list attached to a third,
'brain' class. What I'd like to have happen is when the player presses
a key, the command string is passed to the command handler, which runs
the function. However I can't figure out a good way to make this
happen.

I've tried a dictionary mapping command strings to functions in the
command handler class, but those dictionary values are evaluated just
once when the class is instantiated. I can create a dictionary in a
separate function in the command handler (like a do_command function)
but creating what could be a big dictionary for each input seems kind
of silly (unless I'm misunderstanding something there).

What would be a good way to make this happen, or is there a different
kind of architecture I should be thinking of?



More information about the Python-list mailing list