Frame对象表示执行帧,表示程序运行时函数调用栈中的某一帧。
想要获得某个函数相关的栈帧,则必须在调用这个函数且这个函数尚未返回时获取。可以使用sys模块的_getframe()函数、或inspect模块的currentframe()函数获取当前栈帧。
f_back: 调用栈的前一帧。
f_code: 栈帧对应的code对象。
f_locals: 用在当前栈帧时与内建函数locals()相同,但你可以先获取其他帧然后使用这个属性获取那个帧的locals()。
f_globals: 用在当前栈帧时与内建函数globals()相同,但你可以先获取其他帧……
示例: 假设在下面代码的第四行打断点
函数被断点停止住时刻的frame信息如下
更多Python知识请关注Python视频教程栏目。
Class A(): #class关键字首字母要小写def GetEntryValues(): #类中的方法要添加self作为参数,或者使用staticmethod声明成静态函数InputValue = InputPath.get()print InputValuereturn InputValue def OpenInputValue(): #类中的方法要添加self作为参数print "sdsdsd"GetEntryValues() #调用类中方法的时候,如果是实例方法,需要使用self调用,如果是静态方法,需要类名调用 mainFrame = Tk()frame1 = Frame(mainFrame)frame1.pack()InputPath = Entry(frame1)InputPath.pack(side=LEFT)GoButton = Button(frame1, text='open', command=OpenInputValue) #OpenInputValue是类中的方法,需要使用实例或者类名调用GoButton.pack(side=LEFT)mainFrame.mainloop()Class A(): #class关键字首字母要小写def GetEntryValues(): #类中的方法要添加self作为参数,或者使用staticmethod声明成静态函数InputValue = InputPath.get()print InputValuereturn InputValue def OpenInputValue(): #类中的方法要添加self作为参数print "sdsdsd"GetEntryValues() #调用类中方法的时候,如果是实例方法,需要使用self调用,如果是静态方法,需要类名调用 mainFrame = Tk()frame1 = Frame(mainFrame)frame1.pack()InputPath = Entry(frame1)InputPath.pack(side=LEFT)GoButton = Button(frame1, text='open', command=OpenInputValue) #OpenInputValue是类中的方法,需要使用实例或者类名调用GoButton.pack(side=LEFT)mainFrame.mainloop()