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

dmitrey dmitrey15 at
Fri Jan 6 14:29:16 EST 2012

On Jan 6, 8:28 pm, Ian Kelly < at> wrote:
> On Fri, Jan 6, 2012 at 11:02 AM, dmitrey <dmitre... 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!")
> Cheers,
> Ian

Python build-in function sum() has no attribute func_code, what should
I do in the case?

More information about the Python-list mailing list