Skip to content
Advertisement

How can I read a function’s signature including default argument values?

Given a function object, how can I get its signature? For example, for:

def my_method(first, second, third='something'):
    pass

I would like to get "my_method(first, second, third='something')".

Advertisement

Answer

import inspect

def foo(a, b, x='blah'):
    pass

print(inspect.signature(foo))
# (a, b, x='blah')

Python 3.5+ recommends inspect.signature().

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement