sys._getframe() not behaving as expected

Discussion in 'Python' started by Skip Montanaro, Dec 1, 2003.

  1. 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())

    calling it like so from the interpreter prompt (CVS):

    >>> import gl
    >>> def f():

    ... gl.printframes()
    ...
    >>> f()

    frame[-2]: 3822448
    frame[-1]: 3822448
    frame[0]: 3822448
    frame[-2] globals: 11889232
    frame[-1] globals: 11889232
    frame[0] globals: 11889232
    my globals: 11889232
    >>> id(globals())

    3955136

    The gl.printframes() function never prints the id of the interpreter's
    globals(), nor does it ever print different frames. What am I missing? I
    would like to get a handle on the globals for the frame from which
    printframes() is called.

    Skip
     
    Skip Montanaro, Dec 1, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Kafieh
    Replies:
    7
    Views:
    2,495
    Roedy Green
    May 19, 2004
  2. William Krick
    Replies:
    4
    Views:
    77,869
    rt19983
    Dec 18, 2008
  3. Replies:
    1
    Views:
    348
    David
    Apr 16, 2007
  4. Steve Richter
    Replies:
    2
    Views:
    609
    Steve Richter
    May 29, 2007
  5. Replies:
    2
    Views:
    434
Loading...

Share This Page