function jumptable idiom

Brian E Gallew geek+ at
Thu Mar 9 17:27:25 CET 2000

I'm building a program with an interactive loop in it, along with a
trivial parser.  What I'm doing is this:

command_list = {
    'exit'		: exit,         # No params
    'help'		: help,         # 0 or 1 parameter
    'information'	: vfudata.display # no params
    'output'		: vfudata.write # 0 or 1 parameter

while 1:
    line = sys.stdin.readline()
    if not line: break
    words = split(line)
    if not words: continue
    if not command_list.has_key(words[0]):
        sys.stdout.write('\n\nInvalid command %s\n\n')
    cmd = command_list[words[0]]

However, that means that all of the functions in the jumptable have to
assume that they will always get one argument (a list).  Is there a
more natural way to express this? 

| JAVA must have been developed in the wilds of West Virginia.      |
| After all, why else would it support only single inheritance??    |
| Finger geek at for my public key.                            |
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 266 bytes
Desc: not available
URL: <>

More information about the Python-list mailing list