Get names of positional arguments from function's signature

无人久伴 提交于 2019-12-05 11:47:52

Yes, you can achieve a more robust solution by additionally checking if the .kind attribute of the parameter is not equal to Parameter.VAR_POSITIONAL.

For *args, this is the value that is set when you build the signature object from a function:

>>> def foo(a, b, c=1, *args): pass
>>> print(signature(foo).parameters['args'].kind)
VAR_POSITIONAL

So just import Parameter from inspect and add or the condition that kind != Parameter.VAR_POSITIONAL:

>>> from inspect import Parameter
>>> Parameter.VAR_POSITIONAL == p['args'].kind
True
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!