How to find out the arity of a method in Python

后端 未结 5 1602
滥情空心
滥情空心 2020-11-30 03:48

I\'d like to find out the arity of a method in Python (the number of parameters that it receives). Right now I\'m doing this:

def arity(obj, method):
  retur         


        
5条回答
  •  攒了一身酷
    2020-11-30 04:31

    Use a decorator to decorate methods e.g.

    def arity(method):
    
      def _arity():
        return method.func_code.co_argcount - 1 # remove self
    
      method.arity = _arity
    
      return method
    
    class Foo:
      @arity
      def bar(self, bla):
        pass
    
    print Foo().bar.arity()
    

    Now implement _arity function to calculate arg count based on your needs

提交回复
热议问题