3 Answers
3

Based on my interpretation of your requirements -- you want to dynamically define a function with a signature that matches the content of adict provided at runtime -- there are two issues here which makes it impractical.

If the arguments are defined at run-time, how can your function reference the variables? Are you planning to build the function body at run-time as well?

dicts are unordered, so you cannot reliably use them to define positional arguments

I suspect this is an XY problem. If you can explain what you're trying to achieve then perhaps we can be of better help.

However, assuming you're trying to assign default keyword arguments using a dict then one way to achieve this would be to use decorators. For example:

Thanks for your answer, it helped me accomplish what I want to do. I think it was, as you've stated, an XY problem; I was simply going about things the wrong way. It honestly kind of surprised me that I put myself into this predicament, since the last time I remember something like this happening was when I was a beginner at programming and learning Java for the first time, then being incensed that there wasn't a goto statement (when there were other, better solutions to my problem).
–
LanaruAug 14 '12 at 16:36

1

Glad you found a solution! We've all been in that position and sometimes all it takes is a gentle reminder to take a step back and reformulate the problem.
–
Shawn ChinAug 14 '12 at 16:49

I'm really not sure what you plan on accomplishing here. The following works (sort of):

def func(**dct):
pass
dct = {'foo':0, 'bar':1, 'baz':2}
func(**dct)

How do you plan on using foo, bar or baz in the function if they're created dynamically? If you give some more details on what you're actually trying to accomplish, we might be able to be a little more helpful.