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

Python Semantic Bad

  1. Why List Comprehension is Bad
  2. Python 3: Map with Side Effect Doesn't Work If Result is Not Used
  3. The Idiocy of Python's Function Parameter Specification
  4. Python Scope Complexity, Shallow Copy, Deep Copy, Circular List, and the Garbage Underneath Computer Languages
  5. Python: dict={} vs dict.clear()
  6. Python: Get Number of Arguments of Function
  7. Python: Copy Nested List, Shallow/Deep Copy
  8. Python: Keyword Argument Default Value Unstable
  9. Python: Append String in Loop
  10. FCK Python: String Methods, Functions, Slashes and Backslashes
  11. Python Bitwise Invert Oddity
  12. From Why Not Ruby to FCK Python, Hello Ruby
  13. Why Learn Lisp When There Are Perl and Python

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