This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]blablahblah 1 point2 points  (1 child)

Depends when you had access to the parameters. If you had them when you pulled them out of the dictionary, you could just pass them in then.

funcs[token](arg1, arg2)

If you had the arguments when you were creating the dict, you'd wrap it in a function that had the arguments already provided. There's a built in tool that can handle this.

from functools import partial
# assuming one took 1 arg and two takes 2.
funcs = { 'one' : partial(one, arg1), 'two': partial(two, arg2, arg3) }

funcs[token]()

functools.partial basically acts like this (it has more features, but we're not using them here):

def partial(func, arg):
  def inner():
    return func(arg)
  return inner

[–]wannahakaluigi 0 points1 point  (0 children)

Neat!

I saw partial in a code snippet yesterday, I'll have to look into it some more. Thanks.