I'm new in Python and wants to know if there is a simple way to get amount of passed parameters in Python function.
a(1, 2, 3) ==>3
a(1, 2) ==>2
To get the number of arguments that were passed into your function, call func_num_args() and read its return value. To get the value of an individual parameter, use func_get_arg() and pass in the parameter number you want to retrieve to have its value returned back to you.
Use string isdigit() method to check user input is number or string. Note: The isdigit() function will work only for positive integer numbers. i.e., if you pass any float number, it will not work.
def a(*args, **kwargs):
print(len(args) + len(kwargs))
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With