Given a function object, how can I get its signature? For example, for:
JavaScript
x
3
1
def my_method(first, second, third='something'):
2
pass
3
I would like to get "my_method(first, second, third='something')"
.
Advertisement
Answer
JavaScript
1
8
1
import inspect
2
3
def foo(a, b, x='blah'):
4
pass
5
6
print(inspect.signature(foo))
7
# (a, b, x='blah')
8
Python 3.5+ recommends inspect.signature()
.