Videos
In general the signature of a function is defined by the number and type of input arguments the function takes and the type of the result the function returns.
As an example in C++ consider the following function:
int multiply(int x, int y){
return x*y;
}
The signature of that function, described in an abstract way would be the set {int, int, int}.
As Python is a weakly typed language in general the signature is only given by the amount of parameters. But in newer Python versions type hints were introduced which clarify the signature:
def multiply(x: int, y: int) -> int:
return x*y
The signature indicates the names and types of the input arguments, and (with type annotations) the type of the returned result(s) of a function or method.
This is not particular to Python, though the concept is more central in some other languages (like C++, where the same method name can exist with multiple signatures, and the types of the input arguments determine which one will be called).
import inspect
def foo(a, b, x='blah'):
pass
print(inspect.signature(foo))
# (a, b, x='blah')
Python 3.5+ recommends inspect.signature().
Arguably the easiest way to find the signature for a function would be help(function):
>>> def function(arg1, arg2="foo", *args, **kwargs): pass
>>> help(function)
Help on function function in module __main__:
function(arg1, arg2='foo', *args, **kwargs)
Also, in Python 3 a method was added to the inspect module called signature, which is designed to represent the signature of a callable object and its return annotation:
>>> from inspect import signature
>>> def foo(a, *, b:int, **kwargs):
... pass
>>> sig = signature(foo)
>>> str(sig)
'(a, *, b:int, **kwargs)'
>>> str(sig.parameters['b'])
'b:int'
>>> sig.parameters['b'].annotation
<class 'int'>