0

I have rather strange question about function variable scope and return. Is there any way to inspect the scope of called function in the caller after called function returns value?

Use case is simple: in Flask views I would like to bypass locals() to my templates. I can define what template I need by convention and having return a dictionary in every view bothers me.

6
  • 1
    Your question doesn't make to much sense - you describe two functions, a caller and called, and ask if you can access the scope of the called function after the caller returns a value - the caller can't do anything once it returns (no function can). If you meant the after the called returns, then you would have to store it manually to ensure it was not garbage collected once the function ended. Commented Jan 2, 2013 at 15:44
  • All in all, this kind of thing is generally a bad idea - it leads to accidentally inserting things into scopes you didn't mean to. Explicit is better than implicit. Commented Jan 2, 2013 at 15:46
  • 1
    You better clarify what problem you are trying to solve instead of narrowing down the solution to something related to scope. Commented Jan 2, 2013 at 15:46
  • Thanks, Lattyware, I want to see scope of called function after called function returns. In the caller function. Commented Jan 2, 2013 at 15:47
  • Yes, this is probably bad idea but at this moment I just do not see how this can be made at all. From what I see this is impossible. Commented Jan 2, 2013 at 15:48

1 Answer 1

4

After the function returns, its scope no longer exists. This makes it problematic to get the scope after the function returns.

However, using Python's trace or profile capability, it's possible to run some code just as a function is about to return, and extract the locals from its stack frame at that time. These can then be squirreled away somewhere and returned along with (or instead of) the return value of the called function using a wrapper function.

Here is a decorator that can be used for this nefarious purpose. Keep in mind that this implementation is a horrible hack and it would be bad style to use it for mere convenience. I could probably think of legitimate uses... give me a few days. Also, it may not work with non-CPython implementations (probably won't, in fact).

import sys, functools

def givelocals(func):
    
    localsdict = {}

    def profilefunc(frame, event, arg):
        if event == "call":
            localsdict.clear()
        elif event == "return":
            localsdict.update(frame.f_locals)
        return profilefunc    

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        oldprofilefunc = sys.getprofile()
        sys.setprofile(profilefunc)
        try:
            return func(*args, **kwargs), dict(localsdict)
        except Exception as e:
            e.locals = dict(localsdict)
            raise
        finally:
            sys.setprofile(oldprofilefunc)

    return wrapper

Example:

@givelocals
def foo(x, y):
    a = x + y
    return x * y

>>> foo(3, 4)
(12, {'y': 4, 'x': 3, 'a': 7})

If you have some arbitrary function you want to use it with that you can't decorate because it's in a module you didn't write, you can create the wrapper on the fly and call it:

def foo(x, y):
    a = x + y
    return x * y

>>> givelocals(foo)(3, 4)
(12, {'y': 4, 'x': 3, 'a': 7})

Or store the wrapper and call it later:

locals_foo = givelocals(foo)
>>> locals_foo(3, 4)
(12, {'y': 4, 'x': 3, 'a': 7})

The wrapper returns a tuple of the actual return value and the locals dictionary. If an exception is raised, the .locals attribute of the exception object is set to the locals dict.

Keep in mind that usually Python frees the memory used by the local variables when the function returns. Retaining these values will cause your program to use more memory (for as long as there are references to them), so it'd be a good idea to clean them up when you no longer need them.

One last note: I'm using Python's profile functionality here because it's invoked only at function calls and returns. If you're using a Python version prior to 2.6, you don't have profiling, so you'd need to use tracing instead. The profile function will also work as a trace function as written, you'd just need to use gettrace() and settrace() rather than the corresponding profile-related functions. However, since tracing is called for each line, the wrapped function may be noticeably slower.

Sign up to request clarification or add additional context in comments.

6 Comments

I am having it now, thanks, I just was thinking about some way to not write anything at all (return {} or return locals()).
@peroksid Instead of thinking of ways to avoid typing a small bit now, you should be thinking of ways to make your code more readable and maintainable later. In the end, you will save more typing that way.
I've updated my answer with code that actually does what the OP wants to do.
Awesome. I was hoping for something like this but could not put it together myself. Real masterpiece.
@Phil Frost. Why a convention like "locals() is used as a template context" can create a confusion? This is not just a typing little less, it is views unification.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.