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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.