function jumptable idiom
effbot at telia.com
Thu Mar 9 18:27:42 CET 2000
Brian E Gallew wrote:
Content-Type: multipart/signed; protocol="application/pgp-signature";
(now that's a great way to make sure your messages
don't show up in some agents. maybe you didn't want
the bots to see your post? ;-)
> I'm building a program with an interactive loop in it, along with a
> trivial parser. What I'm doing is this:
> cmd = command_list[words]
> 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?
maybe you could use apply?
cmd = command_list[words]
(don't forget to add try-except to make sure your
program doesn't stop if user forgets an argument
alternatively, you can use the cmd module:
here's an example (from the eff-bot guide; see below for
import string, sys
self.prompt = '> '
def do_hello(self, arg):
print "hello again", arg, "!"
print "syntax: hello [message]",
print "-- prints a hello message"
def do_quit(self, arg):
print "syntax: quit",
print "-- terminates the application"
do_q = do_quit
# try it out
cli = CLI()
and here's some output:
Documented commands (type help <topic>):
> hello world
hello again world !
hope this helps!
<!-- (the eff-bot guide to) the standard python library:
More information about the Python-list