Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the number of arguments of a Python function?

How can I find the number of arguments of a Python function? I need to know how many normal arguments it has and how many named arguments.

Example:

def someMethod(self, arg1, kwarg1=None):     pass 

This method has 2 arguments and 1 named argument.

like image 884
Georg Schölly Avatar asked May 11 '09 12:05

Georg Schölly


People also ask

How many arguments are there in Python?

5 Types of Arguments in Python Function Definition: keyword arguments. positional arguments. arbitrary positional arguments. arbitrary keyword arguments.

How many arguments can a function have in Python?

While a function can only have one argument of variable length of each type, we can combine both types of functions in one argument. If we do, we must ensure that positional arguments come before named arguments and that fixed arguments come before those of variable length.

How can you get total number of arguments passed to a function?

length property provides the number of arguments actually passed to a function. This can be more or less than the defined parameter's count (see Function. length ).


1 Answers

The previously accepted answer has been deprecated as of Python 3.0. Instead of using inspect.getargspec you should now opt for the Signature class which superseded it.

Creating a Signature for the function is easy via the signature function:

from inspect import signature  def someMethod(self, arg1, kwarg1=None):     pass  sig = signature(someMethod) 

Now, you can either view its parameters quickly by string it:

str(sig)  # returns: '(self, arg1, kwarg1=None)' 

or you can also get a mapping of attribute names to parameter objects via sig.parameters.

params = sig.parameters  print(params['kwarg1']) # prints: kwarg1=20 

Additionally, you can call len on sig.parameters to also see the number of arguments this function requires:

print(len(params))  # 3 

Each entry in the params mapping is actually a Parameter object that has further attributes making your life easier. For example, grabbing a parameter and viewing its default value is now easily performed with:

kwarg1 = params['kwarg1'] kwarg1.default # returns: None 

similarly for the rest of the objects contained in parameters.


As for Python 2.x users, while inspect.getargspec isn't deprecated, the language will soon be :-). The Signature class isn't available in the 2.x series and won't be. So you still need to work with inspect.getargspec.

As for transitioning between Python 2 and 3, if you have code that relies on the interface of getargspec in Python 2 and switching to signature in 3 is too difficult, you do have the valuable option of using inspect.getfullargspec. It offers a similar interface to getargspec (a single callable argument) in order to grab the arguments of a function while also handling some additional cases that getargspec doesn't:

from inspect import getfullargspec  def someMethod(self, arg1, kwarg1=None):     pass  args = getfullargspec(someMethod) 

As with getargspec, getfullargspec returns a NamedTuple which contains the arguments.

print(args) FullArgSpec(args=['self', 'arg1', 'kwarg1'], varargs=None, varkw=None, defaults=(None,), kwonlyargs=[], kwonlydefaults=None, annotations={}) 
like image 184
Dimitris Fasarakis Hilliard Avatar answered Sep 27 '22 16:09

Dimitris Fasarakis Hilliard