On this page
inspect.Signature
Prev: inspect.Parameter | Up: inspect | Next: inspect.Traceback
Rel: inspect.ArgInfo, inspect.Arguments, inspect.Attribute, inspect.BlockFinder
Ref: docs.python.org — inspect.Signature
Tags: #public #python3 #stdlib #inspect
Import
import inspect
from inspect import Signature
Signature & Overview
inspect.Signature(parameters=None, *, return_annotation, __validate_parameters__=True)
A Signature object represents the overall signature of a function.
Methods & Attributes
Signature.bind(self, /, *args, **kwargs)— Get a BoundArguments object, that maps the passedargsandkwargsto the function's signature. RaisesTypeErrorif the passed arguments can not be bound.Signature.bind_partial(self, /, *args, **kwargs)— Get a BoundArguments object, that partially maps the passedargsandkwargsto the function's signature.Signature.empty()— Marker object for Signature.empty and Parameter.empty.Signature.format(self, *, max_width=None, quote_annotation_strings=True)— Create a string representation of the Signature object.Signature.from_callable(obj, *, follow_wrapped=True, globals=None, locals=None, eval_str=False, annotation_format=<Format.VALUE: 1>)— Constructs Signature for the given callable object.Signature.parameters(...)—Signature.parametersmethod/property.Signature.replace(self, *, parameters=<class 'inspect._void'>, return_annotation=<class 'inspect._void'>)— Creates a customized copy of the Signature.Signature.return_annotation(...)—Signature.return_annotationmethod/property.
Usage
import inspect
def add(a: int, b: int = 10) -> int:
return a + b
sig = inspect.signature(add)
assert list(sig.parameters) == ["a", "b"]
assert sig.return_annotation is int
Gotchas
- Follows
__wrapped__automatically: Respects@functools.wraps; passfollow_wrapped=Falseto inspect the outer decorator itself.
REPL Inspect
from inspect import Signature
help(Signature)
Linked from
Related notes
- tuts/python3/stdlib_module/inspect/Parameter Linked here; links to this page; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/Traceback Linked here; links to this page; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/inspect/ArgInfo Linked here; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/Arguments Linked here; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/Attribute Linked here; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/BlockFinder Linked here; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/BoundArguments shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/BufferFlags shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/ClassFoundException shared tags: #inspect, #stdlib