sys._getframe() not behaving as expected

Tim Peters tim.one at comcast.net
Mon Dec 1 17:28:51 EST 2003


[Skip Montanaro]
> sys._getframe() is acting peculiar.  At least it doesn't seem to be
> doing what I expect based upon a couple readings of its doc.  Given
> this module (call it 'gl'):
>
>     import sys
> 
>     def printframes():
>         print "frame[-2]:", id(sys._getframe(-2))
>         print "frame[-1]:", id(sys._getframe(-1))
>         print "frame[0]:", id(sys._getframe(0))
>         print "frame[-2] globals:", id(sys._getframe(-2).f_globals)
>         print "frame[-1] globals:", id(sys._getframe(-1).f_globals)
>         print "frame[0] globals:", id(sys._getframe(0).f_globals)
>         print "my globals:", id(globals())
> ...

An argument < 0 is treated like 0.  You want to pass 2 and 1, not -2 and -1.





More information about the Python-list mailing list