function jumptable idiom

smst smstNOsmSPAM at
Thu Mar 9 18:31:30 CET 2000


> 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?

Try the built-in 'apply' function -- its first arg is a function
name, and its second is a tuple of expressions, eg:
>>> apply(operator.add, (1, 2))

Since you have a list of arguments, you can use the 'tuple'
function to convert it.  So for your particular problem you
could use this line:

  apply(cmd, tuple(words[1:]))

Hope this is what you were after.

Steve Tregidgo
Software Developer

* Sent from RemarQ The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

More information about the Python-list mailing list