Python: Get Number of Arguments of Function

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


If you have a question, put $5 at patreon and message me.