1

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.

3
  • 1
    can you explain in detail? Commented Feb 5, 2019 at 5:59
  • What do required arguments and non required arguments mean? How do you want to calculate them? Commented Feb 5, 2019 at 6:04
  • I mean with required Argument in a Function for Example ( def(X, Y = 4: ...), where X is required to run the function. Y wouldn't be, because it has a standard Variable Commented Feb 5, 2019 at 6:16

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.