1

I have a function which has a large number of arguments.

I want to have the names of the arguments available in the help() function, but I want the results as a dict.

At the moment, I have the following:

def myfunc(thisparameter,thatparameter,hostname,password,port,service,url,someotherparameter, etc, andsoforth):
   return some_function(dict(thisparameter=thisparameter,thatparameter=thatparameter,hostname=hostname,port=port,service=service,url=url,someotherparameter=someotherparameter, etc=etc, andsoforth=andsoforth))

I can't just use kwargs, since that means the help() function won't show the parameters. I have a lot of these. I understand that I can use __doc__, but that still shows the parameters as **kwargs.

  • 1
    Maybe I'm missing something, but why not document the actual parameters? The function doesn't appear to be using kwargs at all, so why would you want to document kwargs? – Ixrec Nov 27 '15 at 20:32
  • For clarification, I was asking exactly what @Tibo answered. I wanted it to be documented as if there was no kwargs, but to get the parameters in a dict. locals() is the (a?) correct method to do this. – AMADANON Inc. Nov 29 '15 at 20:18
1

You could use the locals() hack proposed in this SO question, like so:

def myfunc(thisparameter,thatparameter):
  return some_function(**locals())

This allows you to explicit arguments to myfunc and call your implementation without the need to repeat all the arguments.

It is not bulletproof. For a more "secure" version you might want to use inspect.

(As a side note, I'm flagging this question for migration to SO. I'm however not so sure if this is a duplicate as you have the help twist that is not mentioned in the SO question)

  • This is almost exactly the answer I was looking for - minus the **, so some_function gets a dict. – AMADANON Inc. Nov 29 '15 at 20:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.