如何访问不同范围的locals()dict?

时间:2012-03-04 05:24:45

标签: python metaprogramming

我正在编写调试器,我需要访问先前调用的框架

的局部变量

例如,我有一个这样的代码:

def foo():
    ...
    result = bar(args) # bar is some function 
    locals_of_bar = ...#get the locals() dict of the frame in which `bar` was called

编辑:我已更新问题以便更清晰。

1 个答案:

答案 0 :(得分:-1)

你能做的最好的事情是:

def foo():
   print bar_locals()

def bar_locals():
   a=1
   b=2
   return locals()

另外:

您可以执行以下操作:

locals_of_methods = {}

def bar():     #执行功能     全球locals_of_methods     locals_of_methods ['杆':当地人()]     #return

执行bar()后,您将拥有locals_of_methods

中的本地人