import inspect

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

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

Python 3.5+ recommends inspect.signature().

Answer from unutbu on Stack Overflow
๐ŸŒ
Readthedocs
python-forge.readthedocs.io โ€บ en โ€บ latest โ€บ signature.html
Signatures, parameters and return types โ€” forge 18.6.0 documentation
Python callables have a signature: the interface which describes what arguments are accepted and (optionally) what kind of value is returned. ... The function func (above) has the signature (a, b, c). We know that it requires three arguments, one for a, b and c.
๐ŸŒ
Python
peps.python.org โ€บ pep-0362
PEP 362 โ€“ Function Signature Object | peps.python.org
August 21, 2006 - For each parameter accepted by the function it stores a Parameter object in its parameters collection. A Signature object has the following public attributes and methods:
Discussions

python - How can I read a function's signature including default argument values? - Stack Overflow
Example argument: 1 :return: Tuple ... how the function signature would be written in python. :param method: a python method :return: A string similar describing the pythong method signature. eg: "my_method(first_argArg, second_arg=42, third_arg='something')" """ # The return value of ArgSpec is a bit weird, as the list of arguments ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - What is a function signature? - Stack Overflow
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 More on stackoverflow.com
๐ŸŒ stackoverflow.com
Helping people understand function signature syntax - Documentation - Discussions on Python.org
(Bringing a conversation about how to explain slash and star in function signatures from another post which talked about using tags, and a GitHub issue which experimented with tabs to show simplified vs complete signatures). Another possibility is to add a โ€œhelpโ€ icon next to signatures. More on discuss.python.org
๐ŸŒ discuss.python.org
9
July 20, 2024
code golf - Valid python function invocation signatures - Code Golf Stack Exchange
Create a script which generates all possible python function invocation signatures, describing the function arguments as their argument name, if it is passed as a keyword argument, and the type of the argument. To make things fair across languages; let's say: the inputs are three lists; ... More on codegolf.stackexchange.com
๐ŸŒ codegolf.stackexchange.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-get-function-signature
Get Function Signature - Python - GeeksforGeeks
July 15, 2025 - Explanation: Here, co_varnames lists the function's variables and co_argcount indicates the number of positional arguments. In addition to using the inspect module directly, decorators can be used to capture and display function signatures during runtime. This method allows you to add extra behavior, like logging function calls or debugging, without modifying the original function.
๐ŸŒ
Python Like You Mean It
pythonlikeyoumeanit.com โ€บ Module2_EssentialsOfPython โ€บ Functions.html
Basics of Functions โ€” Python Like You Mean It
Thus if you want to return multiple items, then your function must return a single container of those items, like a list or a tuple. # Returning multiple items from a function def bad_f(x): """ return x**2 and x**3""" return x**2 # this code can never be reached! return x**3 def good_f(x): """ return x**2 and x**3""" return (x**2, x**3) ... This should be used sparingly, for exceedingly simple functions that can be easily understood without docstrings. A sequence of comma-separated variable names can be specified in the function signature to indicated positional arguments for the function.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ understanding and utilizing function signatures in python
Understanding and Utilizing Function Signatures in Python - AskPython
June 30, 2023 - In this tutorial, we will first look at what functions are and how they are structured, and how to easily determine the function signature in Python using the signature attribute.
Find elsewhere
๐ŸŒ
Python.org
discuss.python.org โ€บ documentation
Helping people understand function signature syntax - Documentation - Discussions on Python.org
July 20, 2024 - (Bringing a conversation about how to explain slash and star in function signatures from another post which talked about using <abbr> tags, and a GitHub issue which experimented with tabs to show simplified vs complete sโ€ฆ
๐ŸŒ
Chipx86
chipx86.blog โ€บ 2025 โ€บ 07 โ€บ 12 โ€บ a-crash-course-on-python-function-signatures-and-typing
A crash course on Python function signatures and typing
August 8, 2025 - Iโ€™ll talk more about that later, but understanding how it works first requires understanding a bit about how Python sees functions. Pythonโ€™s inspect module is full of goodies for analyzing objects and code, and today weโ€™ll explore the inspect.signature() function.
๐ŸŒ
Pyo3
pyo3.rs โ€บ main โ€บ function โ€บ signature
Function signatures - PyO3 user guide
The following IPython output demonstrates how this generated signature will be seen from Python tooling: >>> pyo3_test.add.__text_signature__ '(a, b=..., /)' >>> pyo3_test.add? Signature: pyo3_test.add(a, b=0, /) Docstring: This function adds two unsigned 64-bit integers.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-get-function-signature
Python - Get Function Signature
July 18, 2023 - Function: calculate_area Signature: (length: float, width: float = 1.0) -> float Return type: <class 'float'> length: <class 'float'> width: <class 'float'> (default: 1.0)
๐ŸŒ
Adgefficiency
adgefficiency.com โ€บ blog โ€บ python-function-signature-contracts
Python Function Signatures as Contracts | ADGEfficiency
The second solution to tightening function signature contracts is to use TypeVar from the typing module to create generic functions that preserve type information through the function. Typevar allows polymorphic contracts - generic functions that can work with different return types. Type hints lose information when functions return the same type they receive: ... def first(items: list) -> object: return items[0] numbers = [1, 2, 3] result = first(numbers) # Type: object print(result + 1) # Type error: can't add object + int
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ inspect.html
inspect โ€” Inspect live objects
... Arrange the given list of classes into a hierarchy of nested lists. Where a nested list appears, it contains classes derived from the class whose entry immediately precedes the list.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ using / and * in python function signatures
r/learnpython on Reddit: Using / and * in Python function signatures
August 2, 2024 -

Hi everyone,

I've been exploring the use of / and * in Python function signatures to specify positional-only and keyword-only arguments. I understand the syntax, but I'm curious about their practical implications and best practices. I am coding for more than two years now and have never used them myself and i see them really rarely. That's why would like to learn about others experiance and maybe get some advice when it is appropriate to use them in my own functions?

Looking forward to your insights! Thanks!

Top answer
1 of 2
5
Personally, I use keyword only when the parameter in question is "special" in some way and I want the usage more clear and also give me headroom to change the API later. Consider something like this: def create_pdf_report(foo, bar, *, debug=False) -> bytes Here, foo and bar are some parameters that will be used in the created PDF report, while the debug flag will cause a special PDF to be created which contains debug information (layout lines being drawn, boxes around paragraphs etc.) First, I don't want people to call the function as create_pdf_report(some_data, some_more_data, True) because it's not clear what Truemeans in this case, you'd have to check the signature of the create_pdf_report function to figure out it turns on debugging. create_pdf_report(some_data, some_more_data, debug=True) makes it obvious, even to someone not familiar with the API, that this is a report with some debugging info which should probably not make it into production. Another advantage is that in the future, if the API ever needs to change and a third parameter is added: create_pdf_report(foo, bar, baz, *, debug=True), old code will definitely raise an exception, while without keyword-only, create_pdf_report(data, more_data, True) will not, especially not if baz happens to be a boolean parameter. I've never used positional-only args. I know what they are for, I just never felt it necessary.
2 of 2
2
From a module standpoint (disclaimer: I have no experience writing modules - this is mostly speculation and regurgitation from memory), it's useful for providing a consistent API. Positional-only is useful for defining an interface when the library is written in C or similar (or even if you've just picked parameter names that might change). I believe that Python functions written in C can only accept positional arguments (I think PEP 570 explains it well). Keyword-only can make modules more flexible for backwards compatibility. By ensuring that arguments can only be passed by name, it makes the interface consistent (otherwise you've got two options for calling) and probably makes it easier to add or deprecate arguments
๐ŸŒ
IPython
ipython.org โ€บ ipython-doc โ€บ 3 โ€บ api โ€บ generated โ€บ IPython.utils.signatures.html
Module: utils.signatures โ€” IPython 3.2.1 documentation
Function signature objects for callables. Back port of Python 3.3โ€™s function signature tools from the inspect module, modified to be compatible with Python 2.7 and 3.2+.
๐ŸŒ
Sorokin
sorokin.engineer โ€บ posts โ€บ en โ€บ python_decorator_function_signature.html
Python decorators - how to keep function signature (set of arguments) unchanged
import inspect def my_decorator(original_function): def wrapper(*args, **kwargs): try: return original_function(*args, **kwargs) finally: some_clean_up() wrapper.__signature__ = inspect.signature(original_function) return wrapper ยท The arguments list is not the only function feature that we lost after our decorator.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python function signature
How to Get Function Signature | Delft Stack
March 11, 2025 - This tutorial explores how to get function signatures in Python, providing clear methods and examples for understanding function call details. Learn to use the inspect module, __doc__ attribute, and help function to retrieve valuable information about functions.
๐ŸŒ
Smarie
smarie.github.io โ€บ python-makefun
makefun
Note that Signature objects do not contain any function name information. You therefore have to provide an explicit func_name argument to @with_signature (or to create_function) as shown above. ... In python 2 the inspect package does not provide any signature-related features, but a complete backport is available: funcsigs.
๐ŸŒ
Plain English
python.plainenglish.io โ€บ pythons-function-signatures-5-practical-tips-to-craft-elegant-code-b5d368cd2985
Python Function Signatures: 5 Tips for Code Excellence | Python in Plain English
June 5, 2023 - A good design function is not only simple to use but also difficult to misunderstand and misuse. In this post, we will look at five ways to improve the signature of a function in Python.