你怎么知道通过getattr()检索的函数有多少参数?

时间:2016-01-29 14:32:10

标签: python-2.7

如果您通过getattr()检索了某个属性,那么如何检查:

  • 这是一个功能
  • 此函数需要多少个参数

1 个答案:

答案 0 :(得分:1)

In [50]: class Foo:
    def __init__(self):
        self.a = 4
    def somefunc(self, x, y):
        self.a = x+y
   ....:         

In [51]: type(getattr(Foo, 'somefunc'))
Out[51]: function

In [52]: getattr(Foo, 'somefunc').__code__.co_argcount
Out[52]: 3