0

I would like to know if it is possible, given a function (as an instance, or a string), to get its paramaters, if defined default values for each paramater and, if possible, the type of each parameters (probably using the type of default value, if defined) in Python 3.5.

Why would you want that ?!

Long story short, I am generating a XML file containing details of different functions in my project. Since the generator has to be future-proof in case someone modifies, add, or delete a function, the next generated file must be updated. I succesfully retrieved the functions I wanted either as instance or a string of the code calling it.

I have two solutions (well, more the beginnings of solutions) to solve this problem, using inspect and jedi.

Inspect

Using inspect.signature(function), I can retrieve the name and default values of all the parameters. The main issue I see here, would be analyzing this function:

def fct(a=None):
    # Whatever the function does...

Analyzing the type of the default value will lead to misunderstandigs. Is there a way to fix that ?

Jedi

Jedi is an extremely powerful tool, maybe even too much ! Getting the function in a one line code string, and analyzing it through Jedi gives an extraordinary amount of information, that I am lost with to be completely honest. Plus, I might get bad autocompletion (example: instead of having the paramaters for print, I might get autocompleted to println)

If someone had used one of these tools for this prupose, or even better if you know a better, more "pythonic" way of doing this, I would be really grateful !

Community
  • 1
  • 1
superpg
  • 253
  • 2
  • 9
  • https://stackoverflow.com/questions/12627118/get-a-function-arguments-default-value – Daniel Messias Apr 26 '18 at 12:17
  • You could use [type hints](https://www.python.org/dev/peps/pep-0484/) for this, but the question seems too broad for SO generally. – jonrsharpe Apr 26 '18 at 12:17
  • @DanielMessias Thanks, I haven't find this in my research, since I did not only require default values I guess. Still, won't I encounter an issue with ′def fct(a=None)′ ? – superpg Apr 26 '18 at 12:27
  • @jonrsharpe I don't have the right to change all the functions of the project unfornately. Type hints would solve everything in my case! – superpg Apr 26 '18 at 12:28
  • I don't understand the question. `inspect` does what you describe in the preamble of your question. Then you say you tried `inspect` but claim "Analyzing the type of the default value will lead to misunderstandigs". This is where you lose me. What's wrong with `None`? – timgeb Apr 26 '18 at 12:34
  • @timgeb Analyzing the type of `None` will determine that the paramater would like a variable of type "NoneType", while it might be entirely something else. I can simply add a if statement to manage this case, yet I wanted to see if a solution allowed me to determine the "best" type possible for this parameter. It's probably impossible, but you never know !.. – superpg Apr 26 '18 at 12:41
  • @superpg ah, den just juse type hints, maybe leveraging the `typing` module. – timgeb Apr 26 '18 at 12:43

0 Answers0