2009-10-09 16 views
2
private ScriptEngine pe; 
private ScriptScope scope; 

private void button1_Click(object sender, EventArgs e) 
{ 
    pe = Python.CreateEngine(); 
    scope = pe.CreateScope(); 

    pe.Runtime.SetTrace(OnTraceback); 
    pe.Execute(@" 
def square(x): 
    s = x*x 
    return s 

a = square(2) + (3) 
print ""hello world "" + str(a) 
", scope); 
} 

private TracebackDelegate OnTraceback(TraceBackFrame frame, string result, object payload) 
{ 
    //How can I access the local variables of the current function? 

    return OnTraceback; 
} 

讓我再說一遍:我想要得到的功能,其中蟒蛇執行目前的局部變量(函數週圍線frame.f_lineno)。IronPython的:如何訪問追溯局部變量

回答

1

你可以訪問frame.f_locals,它可以讓你獲取/設置所有的變量。

+0

請逐步通過上面的代碼,... frame.f_locals不包含當地人 – ulrichb 2009-10-10 00:02:51

+0

如果我改變OnTraceback返回它的自我,我看到方形函數內的本地人 - 儘管全局似乎沒有。 – 2009-10-10 00:54:22