如何找到 Python 函数的参数数量? [英] How can I find the number of arguments of a Python function?
问题描述
如何找到 Python 函数的参数数量?我需要知道它有多少个普通参数和多少个命名参数.
How can I find the number of arguments of a Python function? I need to know how many normal arguments it has and how many named arguments.
示例:
def someMethod(self, arg1, kwarg1=None):
pass
此方法有 2 个参数和 1 个命名参数.
This method has 2 arguments and 1 named argument.
推荐答案
之前接受的答案是 不推荐使用,从 Python 3.0
开始.您现在应该选择取代它的 Signature
类,而不是使用 inspect.getargspec
.
The previously accepted answer has been deprecated as of Python 3.0
. Instead of using inspect.getargspec
you should now opt for the Signature
class which superseded it.
from inspect import signature
def someMethod(self, arg1, kwarg1=None):
pass
sig = signature(someMethod)
现在,您可以通过 str
快速查看其参数:
Now, you can either view its parameters quickly by str
ing it:
str(sig) # returns: '(self, arg1, kwarg1=None)'
或者您也可以通过 sig.parameters
获得属性名称到参数对象的映射.
or you can also get a mapping of attribute names to parameter objects via sig.parameters
.
params = sig.parameters
print(params['kwarg1']) # prints: kwarg1=20
此外,您可以在 sig.parameters
上调用 len
来查看此函数需要的参数数量:
Additionally, you can call len
on sig.parameters
to also see the number of arguments this function requires:
print(len(params)) # 3
params
映射中的每个条目实际上都是一个 Parameter
对象 具有更多属性,使您的生活更轻松.例如,现在可以轻松地获取参数并查看其默认值:
Each entry in the params
mapping is actually a Parameter
object that has further attributes making your life easier. For example, grabbing a parameter and viewing its default value is now easily performed with:
kwarg1 = params['kwarg1']
kwarg1.default # returns: None
对于参数
中包含的其余对象也是如此.
similarly for the rest of the objects contained in parameters
.
对于 Python 2.x
用户来说,虽然 inspect.getargspec
没有 被弃用,但该语言很快就会:-).Signature
类在 2.x
系列中不可用,也不会.所以你仍然需要使用 inspect.getargspec
.
As for Python 2.x
users, while inspect.getargspec
isn't deprecated, the language will soon be :-). The Signature
class isn't available in the 2.x
series and won't be. So you still need to work with inspect.getargspec
.
至于在 Python 2 和 3 之间的转换,如果您的代码依赖于 Python 2 中的 getargspec
接口并切换到 3
signature
/code> 太难了,你确实有使用 ="noreferrer">inspect.getfullargspec
.它提供了与 getargspec
(单个可调用参数)类似的接口,以便获取函数的参数,同时还处理一些 getargspec
没有的其他情况:>
As for transitioning between Python 2 and 3, if you have code that relies on the interface of getargspec
in Python 2 and switching to signature
in 3
is too difficult, you do have the valuable option of using inspect.getfullargspec
. It offers a similar interface to getargspec
(a single callable argument) in order to grab the arguments of a function while also handling some additional cases that getargspec
doesn't:
from inspect import getfullargspec
def someMethod(self, arg1, kwarg1=None):
pass
args = getfullargspec(someMethod)
与 getargspec
一样,getfullargspec
返回一个包含参数的 NamedTuple
.
As with getargspec
, getfullargspec
returns a NamedTuple
which contains the arguments.
print(args)
FullArgSpec(args=['self', 'arg1', 'kwarg1'], varargs=None, varkw=None, defaults=(None,), kwonlyargs=[], kwonlydefaults=None, annotations={})
这篇关于如何找到 Python 函数的参数数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!