我如何找到Python函数的参数个数? [英] How can I find the number of arguments of a Python function?

查看:512
本文介绍了我如何找到Python函数的参数个数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到Python函数的参数数量?我需要知道它有多少个正常参数以及多少个命名参数。



示例:

  def someMethod(self,arg1,kwarg1 = None):
pass

这个方法有2个参数和1个命名参数。

以前接受的答案是 弃用 Python 3.0 。现在,您应该选择替代它的 Signature 类,而不是使用 inspect.getargspec

通过 签名功能

 从检查导入签名

def someMethod(self,arg1,kwarg1 = None):
pass
$ b $ sig =签名(someMethod)

现在,您可以快速查看其参数 str

  str(sig)#返回:'(self,arg1,kwarg1 = None)'

或者您还可以通过 sig.parameters

  params = sig.parameters 
print(params ['kwarg1'])#prints:kwarg1 = 20

另外,您可以在<$ c上调用 len $ c> sig.parameters 也可以看到这个函数需要的参数个数:

  print( len(params))#3 

params mapping实际上是一个 参数 code>对象,它有更多的属性让你的生活更轻松。例如,抓取一个参数并查看它的默认值现在很容易执行:

  kwarg1 = params ['kwarg1'] 
kwarg1.default#返回:无

类似地,包含在参数






至于Python 2。 x 用户,而 inspect.getargspec 不被使用,该语言很快就会变成:-)。 Signature 类在 2.x 系列中不可用,不会有。因此,您仍然需要使用 inspect.getargspec



至于Python 2和Python 3之间的转换,如果您的代码依赖于 Python 2中的getargspec 并且在 3 中切换到签名太困难了,您确实有宝贵的选择使用 inspect.getfullargspec 。它提供了一个与 getargspec (一个可调用参数)相似的接口,以便获取函数的参数,同时还处理 getargspec 不会:

  from inspect import getfullargspec 

def someMethod(self ,arg1,kwarg1 = None):
pass

args = getfullargspec(someMethod)

getargspec 一样, getfullargspec 返回一个 NamedTuple

  print(args)
FullArgSpec(args = ['self',' arg1','kwarg1'],varargs = None,varkw = None,defaults =(None,),kwonlyargs = [],kwonlydefaults = None,annotations = {})
pre>

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.

Example:

def someMethod(self, arg1, kwarg1=None):
    pass

This method has 2 arguments and 1 named argument.

解决方案

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.

Creating a Signature for the function is easy via the signature function:

from inspect import signature

def someMethod(self, arg1, kwarg1=None):
    pass

sig = signature(someMethod)

Now, you can either view its parameters quickly by string it:

str(sig)  # returns: '(self, arg1, kwarg1=None)'

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

Additionally, you can call len on sig.parameters to also see the number of arguments this function requires:

print(len(params))  # 3

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.


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.

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)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆