Complete worked example
python
from functools import wraps
import inspect
def traced(function):
@wraps(function)
def wrapper(*args, **kwargs):
return function(*args, **kwargs)
return wrapper
@traced
def scale(value, *, factor=2):
"""Scale one value."""
return value * factor
def inspect_example(value, factor):
return {'value': scale(value, factor=factor), 'name': scale.__name__,
'parameters': list(inspect.signature(scale).parameters)}- Reasoning
- Start at the decorated scale definition, then follow one request through wrapper into the original function. Three with factor four returns twelve. The wrapper accepts arbitrary argument shapes so it can forward them, while inspect.signature follows the wrapped relationship to describe the original public parameters.