function jumptable idiom

smst smstNOsmSPAM at bigfoot.com.invalid
Thu Mar 9 18:31:30 CET 2000


Brian,

> 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))
3

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.

Cheers,
Steve Tregidgo
Software Developer
http://www.businesscollaborator.com



* Sent from RemarQ http://www.remarq.com 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