Wednesday, June 23, 2010

Python: Convert args to dict?

Programmer Question

How would I write a function that I can call like:



package(var1, var2, var3)


Edit: This syntax would be fine too:



package('var1','var2','var3')


Although the former would be preferred (fewer quotes to type).



And it would return a dict:



{'var1':var1, 'var2':var2, 'var3':var3}


Is this possible? Or is the variable name lost when passed into a function with a signature like package(*args)?



Edit: Just like PHP's compact function.



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails