-1

I'm trying to access the frame information for a particular call to the list.sort method, but i am having trouble.

import inspect

def calm_pear(x): # compare    
    cur_frame = inspect.currentframe()
    out_frames = inspect.getouterframes(cur_frame)
    print(out_frames[0].function) # prints 'calm_pear'
    print(out_frames[1].function) # prints 'inner'  
    print(out_frames[2].function) # prints 'outer'
    return id(x)

def outer():
    inner()

def inner():
    [0, 1].sort(key=calm_pear)

outer() # call outer

The print-out I get is:

    calm_pear
    inner

However, the call-order is (outer, inner, list.sort, calm_pear)

Why isn't out_frames[1].function something like list.sort?

Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42

1 Answers1

1

list.sort is written in C. Functions written in C don't get Python stack frames. Python stack frames are only needed to execute code written in Python.

Whatever you were hoping to do by accessing list.sort's nonexistent stack frame, you will have to find some other way to do it.

user2357112
  • 260,549
  • 28
  • 431
  • 505