inspect.stack() and frame

Félix-Antoine Fortin felix.antoine.fortin at gmail.com
Fri Mar 12 00:05:00 CET 2010


Given this code :
# Experience with frame
import sys
import inspect

def foo():
    stack = inspect.stack()
    print "foo frame : " + str(hex(id(sys._getframe())))

def foo2():
    inspect.stack()
    print "foo2 frame : " + str(hex(id(sys._getframe())))

def bar():
    print "bar frame : " + str(hex(id(sys._getframe())))

foo()
foo()

foo2()
foo2()

bar()
bar()

Output example :
foo frame : 0x84d2c0
foo frame : 0x844bf0
foo2 frame : 0x898c90
foo2 frame : 0x898c90
bar frame : 0x898f70
bar frame : 0x898f70

Why are the ids (address) of the frame for each foo call not the same?
Or why the call to "stack = inspect.stack()" change the address of the
frame?



More information about the Python-list mailing list