Using Python 3.x, I'm trying to get the name of all positional arguments from some function i.e:
def foo(a, b, c=1):return
Right now I'm doing this:
from inspect import signature, _empty
args =[x for x, p in signature(foo).parameters.items() if p.default == _empty]
When the function alows *args i.e:
def foo(a, b, c=1, *args):return
I'm adding the line:
args.remove("args")
I was wondering if there is a better way to achieve this.
As suggested by Jim Fasarakis-Hilliard one better way to deal with *args case is using Parameter.kind:
from inspect import signature, Parameter
args =[]
for x, p in signature(foo).parameters.items():if p.default == Parameter.empty and p.kind != Parameter.VAR_POSITIONAL:args.append(x)