Check the number of parameters passed in Python function

You can do this by using locals()

It is important to note, that this should be done as ultimately, your first step in your method. If you introduce a new variable in your method, you will change your results. So make sure you follow it this way:

def a(a, b, c):
    # make this your first statement
    print(len(locals()))

If you did this:

def a(a, b, c):
    z = 5
    print(len(locals()))

You would end up getting 4, which would not be right for your expected results.

Documentation on locals()


you could also change the input for your function to a list, so to have a function:

a(your_list)

then to know how many arguments have been passed to the function, you could simply do:

print len(your_list)

However, this means that you change the input type for your function, from many input variables to only one, the list(which can have a variable number of elements).


def a(*args, **kwargs):
  print(len(args) + len(kwargs))