What you're asking for doesn't really make sense.
A function's local variables don't have values all the time. Consider this function:
def foo(x):
y = x + 27
return y
What is the value of foo
's local variable y
? You can't answer, the question doesn't even make sense until you call foo
(and even then, not until the line y = x + 27
is executed).
And even then it's not just that y
might not have a value at this moment, there could be any number of "in flight" executions of foo
. There could be threads executing foo
, or foo
could be recursive (possibly indirectly) so that there is more than one call in progress even in a single call stack. Or foo
could be a generator, so there could be many in-flight foo
executions even without recursion (i.e. they're not all reachable from some outermost foo
scope). So which y
would you get the value of?
The value of y
in foo
just isn't a well-defined concept unless you're talking about within the scope of foo
.
Given Python's flexibility, I'm pretty sure it's possible to do stack frame introspection and find a stack frame for foo
when there is one currently live and pull out the values of its local variables at that time. This would be pretty hard (if not impossible) to do with a decorator, because (unless foo
is a generator) the decorator can only add wrapper code "around" foo
, which means the code controlled by the decorator runs before and after foo
runs, so you only get control when foo
's stack frame doesn't exist.
I'm not going to give specific pointers on exactly how to do this, because I don't know how to do it. It sounds like it's almost certainly a bad idea though, unless you're writing a debugger.