How to find the number of arguments in a Python function?

In this article, we are going to see how to count the number of arguments of a function in Python. We will use the special syntax called *args that is used in the function definition of python. Syntax *args allow us to pass a variable number of arguments to a function. We will use len() function or method in *args in order to count the number of arguments of the function in python.
Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

def no_of_argu(*args):
      
    # using len() method in args to count
    return(len(args))
  
  
a = 1
b = 3
  
# agruments passed
n = no_of_argu(1, 2, 4, a)
  
# result printed
print(" The number of arguments are: ", n)

chevron_right


Output :  

The number of arguments passed are: 4

Example 2:


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

def no_of_argu(*args):
    
    # using len() method in args to count
    return(len(args))
  
print(no_of_argu(2, 5, 4))
print(no_of_argu(4, 5, 6, 5, 4, 4))
print(no_of_argu(3, 2, 32, 4, 4, 52, 1))
print(no_of_argu(1))

chevron_right


Output :


3
6
7
1



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.