function jumptable idiom

Brian E Gallew geek+ at cmu.edu
Thu Mar 9 11:27:25 EST 2000


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

[chomp]
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:
    sys.stdout.write(sys.ps1)
    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')
        continue
    cmd = command_list[words[0]]
    cmd(words[1:])

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 cmu.edu 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: <http://mail.python.org/pipermail/python-list/attachments/20000309/13cd9269/attachment.sig>


More information about the Python-list mailing list