dictionary that have functions with arguments

Neal Norwitz nnorwitz at gmail.com
Wed Nov 2 00:58:36 EST 2005


Ron Adam wrote:
>
> Eval or exec aren't needed.  Normally you would just do...
>
>     execfunc['key1'](**args)
>
> If your arguments are stored ahead of time with your function...
>
>     Committed revision 41366.

>
> You could then do...
>
>     func, args = execfunc['key1']
>     func(**args)

Interesting that both Ron and Alex made the same mistake.  Hmmm, makes
me wonder if they are two people or not...

If args is a tuple, it should be:

  func(*args)

If you want the full generality and use keyword args:

  func(*args, **kwargs)

kwargs would be a dictionary with string keys.

E.g.,

  execfunc = {'key1':(func1, (1,), {'keyarg': 42})}

HTH,
n




More information about the Python-list mailing list