Python的函数的参数长度? [英] Length of arguments of Python function?

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

问题描述


  

可能重复:结果
  如何找出一个方法在元数Python的


例如我宣布一个函数:

  DEF总和(A,B,C):
    返回+ B + C

我想要得到的求和函数的参数长度。

somethig是这样的:some_function(总和)退换3

如何可以把它在Python做呢?

更新:

因为我想写一个接受另一个函数作为参数,参数的函数通过它我问这个问题。

 高清FUNCT(anotherFunct,**参数):

和我需要验证:

  IF(LEN(参数)= anotherFuct.func_ code.co_argcount!):
    回归错误


解决方案

如果您的方法名称为然后 sum.func_ code .co_argcount 会给你的参数个数。

Possible Duplicate:
How to find out the arity of a method in Python

For example I have declared a function:

def sum(a,b,c):
    return a + b + c

I want to get length of arguments of "sum" function.
somethig like this: some_function(sum) to returned 3
How can it be done in Python?

Update:
I asked this question because I want to write a function that accepts another function as a parameter and arguments to pass it.

def funct(anotherFunct, **args): 

and I need to validate:

if(len(args) != anotherFuct.func_code.co_argcount):
    return "error"

解决方案

If your method name is sum then sum.func_code.co_argcount will give you number of arguments.

这篇关于Python的函数的参数长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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