Search code examples
pythonparameterssignatureargs

How to determine the number of arguments passed to function?


I have a function that takes a variable number of arguments. I would like (in the function) get the number passed. The declaration looks like this:

def function(*args):

The function is called as follows:

function(arg1, arg2, ...)

Solution

  • You can define the function as follows to get the values of the arguments:

    def function(*args):
       for i in range(len(args)):
          print(args[i])
    
    function(3, 5, 8)
    

    Here is the result:

    3
    5
    8
    

    That is, args[i] receives the value of the i-th parameter passed to function.