MCPcopy Create free account
hub / github.com/bslatkin/effectivepython / trace_func

Function trace_func

example_code/item_066.py:53–76  ·  view source on GitHub ↗
(func)

Source from the content-addressed store, hash-verified

51from functools import wraps
52
53def trace_func(func):
54 if hasattr(func, "tracing"): # Only decorate once
55 return func
56
57 @wraps(func)
58 def wrapper(*args, **kwargs):
59 args_repr = repr(args)
60 kwargs_repr = repr(kwargs)
61 result = None
62 try:
63 result = func(*args, **kwargs)
64 return result
65 except Exception as e:
66 result = e
67 raise
68 finally:
69 print(
70 f"{func.__name__}"
71 f"({args_repr}, {kwargs_repr}) -> "
72 f"{result!r}"
73 )
74
75 wrapper.tracing = True
76 return wrapper
77
78
79print("Example 2")

Callers 2

__new__Method · 0.85
traceFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected