Reputation: 169
I was studying python and I ran into the concept of 'signature'. I looked things up but signature of a function seems to have different meaning for different languages. So, what does signature of a function refer to in python?
Upvotes: 10
Views: 31651
Reputation: 100
A function signature is its declaration, parameters, and return type.
def func(params):
#some stuff
return modified_params
When you call it is an instance of that function.
var = func(parameters)
Upvotes: 0
Reputation: 373
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
Upvotes: 19
Reputation: 189457
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).
Upvotes: 4