how to get id(function) for each function in stack?

Ian Kelly ian.g.kelly at
Fri Jan 6 13:28:21 EST 2012

On Fri, Jan 6, 2012 at 11:02 AM, dmitrey <dmitrey15 at> wrote:
> hi all,
> how to get id(func) for each func in stack? (I mean memory address, to
> compare it with id(some known funcs))
> Thank you in advance, D.

The answer hasn't changed since your last thread about this.  The
stack contains code objects, not functions.  You can get the code
objects using inspect.stack(), and compare them to the func_code
attributes of the functions you're interested in.

Also, there's no need to use id() for this.  Just use the "is"
operator to check identity.

for frame_tuple in inspect.stack():
    frame = frame_tuple[0]
    if frame.f_code is some_function.func_code:
        print("Found it!")


More information about the Python-list mailing list