I'd like to be able to tell if a function was declared as f()
or f(**kwargs)
in Python 2.7, so I know whether or not it can accept arbitrary keyword arguments... but invoke.getargspec.args() returns [] in both cases. Is there a way of doing this?
You need to look at inspect.getargspec().keywords
instead. If it is None
no **kwargs
was given.
From the inspect.getargspec()
documentation:
varargs and keywords are the names of the
*
and**
arguments orNone
.
Demo:
>>> import inspect
>>> def foo(): pass
...
>>> def bar(**kwargs): pass
...
>>> inspect.getargspec(foo).keywords is None
True
>>> inspect.getargspec(bar).keywords is None
False
>>> inspect.getargspec(bar).keywords
'kwargs'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With