How to use signature() function In Python
We can get function Signature with the help of signature() Function. It takes callable as a parameter and returns the annotation. It raises a value Error if no signature is provided. If the Invalid type object is given then it throws a Type Error.
Syntax:
inspect.signature(callable, *, follow_wrapped=True)
Example 1:
from inspect import signature # declare a function gfg with some # parameter def gfg(x: str , y: int ): pass # with the help of signature function # store signature of the function in # variable t t = signature(gfg) # print the signature of the function print (t) # print the annonation of the parameter # of the function print (t.parameters[ 'x' ]) # print the annonation of the parameter # of the function print (t.parameters[ 'y' ].annotation) |
Output
(x:str, y:int) x:str <class 'int'>
Python – Get Function Signature
Let’s consider a scenario where you have written a very lengthy code and want to know the function call details. So what you can do is scroll through your code each and every time for different functions to know their details or you can work smartly. You can create a code where you can get the function details without scrolling through the code. This can be achieved in two ways –
- Using signature() function
- Using decorators