Python: Get Function Arity

By Xah Lee. Date: . Last updated: .

in Python, it is possible get a function's number of args, but only for user-defined functions.

# python 2
# get number of args of a function. works for user-defined function only

def get_number_of_args(func):
    return len(func.func_code.co_varnames)

def g(a, b): pass

print (get_number_of_args(g))   # 2