0

Let's say I have the following function:

def function(a, b):
    pass

How do I get the number of parameters of this function in code. We know it's two, but I want to automatically store it in a variable.

I searched on the internet a little bit, and I only found that there's a thing called "inspect.signature". For example

def function(a, b):
    pass
c = inspect.signature(function)
print(c) # program will print (a, b)

But how do I get the number of arguments from this? I am pretty new to Python. I only see that "inspect.signature" is a class.

1 Answers1

2

You can use c.parameters:

def function(a, b):
    pass
c = inspect.signature(function)
print(len(c.parameters)) # program will print 2
mousetail
  • 7,009
  • 4
  • 25
  • 45