Reputation: 1865
I want to get the Number of required Arguments and non. I would be really happy if someone could help me, I am kinda stuck here.
I have tried following, I don't know if that is the right way to go for it.
from inspect import signature
def Test(X, Y = 4):
print(X,Y)
R = str(signature(Test))
cu = ""
for i in R:
if i.isalpha():
cu = "".join([cu,i])
print(len(cu))
#Output: 2 (I would like to have something like req_Arg = 1, non_req_Arg = 1)
I am using py 3.0
Thank you for suggestions in advance.
Upvotes: 1
Views: 203
Reputation: 21368
You can get the type of parameter and whether or not it has a default by iterating through inspect.signature(Test).parameters.values()
:
>>> for param in inspect.signature(Test).parameters.values():
... print(param.kind, param.default)
...
POSITIONAL_OR_KEYWORD <class 'inspect._empty'>
POSITIONAL_OR_KEYWORD 4
(https://docs.python.org/3.4/library/inspect.html#inspect.Parameter)
It'd be relatively trivial to translate that into the output you're looking for (leaving the exercise up to you).
Upvotes: 1