def foo():
    a = 1
    b = 2
    dir() # prints [a, b]
    bar(?) 
der bar(foo_pointer):
    print dir(foo_pointer) # should print [a,b]
I was trying to use
bar(sys.modules[__name__].main), but that gives not [a,b], but ['__call__','__class__' ...] without a and b.
I actually want to safe that pointer to use later, so i can't just pass the [a,b].
Use the sys._getframe() function to get access to the calling frame. Frame objects have a f_locals attribute giving you access to the local variables of that frame:
import sys
def bar():
    caller = sys._getframe(1)
    print caller.f_locals
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With