[Python] How to get function signature

How to get function signature in Python

Use:

import inspect
function_signature = inspect.signature(function_reference)

Full Example

#
# [Python] How to get function signature
#
import inspect

def myfunc(a, b, multiplier=1.0):
    return multiplier*(a + b)

print("myfunc(1,2) is:", myfunc(1,2))

sig = inspect.signature(myfunc)

msg = "function signature is: " + str(sig)
print(msg)

Note that the signature sig above is not a string (a Python str object). Must be cast to a Python str to concatenate with a string as shown in computing msg.

Python demonstration code output

(C) 2022 by John F. McGowan, Ph.D.

About Me

John F. McGowan, Ph.D. solves problems using mathematics and mathematical software, including developing gesture recognition for touch devices, video compression and speech recognition technologies. He has extensive experience developing software in C, C++, MATLAB, Python, Visual Basic and many other programming languages. He has been a Visiting Scholar at HP Labs developing computer vision algorithms and software for mobile devices. He has worked as a contractor at NASA Ames Research Center involved in the research and development of image and video processing algorithms and technology. He has published articles on the origin and evolution of life, the exploration of Mars (anticipating the discovery of methane on Mars), and cheap access to space. He has a Ph.D. in physics from the University of Illinois at Urbana-Champaign and a B.S. in physics from the California Institute of Technology (Caltech).