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

Ian Kelly ian.g.kelly at gmail.com
Fri Jan 6 15:37:51 EST 2012


On Fri, Jan 6, 2012 at 12:56 PM, Dave Angel <d at davea.name> wrote:
> On 01/06/2012 02:29 PM, dmitrey wrote:
>>
>> On Jan 6, 8:28 pm, Ian Kelly<ian.g.ke... at gmail.com>  wrote:
>>>
>>> On Fri, Jan 6, 2012 at 11:02 AM, dmitrey<dmitre... at gmail.com>  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?
>> D.
>
> flag = False
>
>
> for frame_tuple in inspect.stack():
>    frame = frame_tuple[0]
>    if frame.f_code is some_function.func_code:
>        flag = True
>
> if !flag:
>    print "FuncDesigner.sum() not used.  Change."

That would also print when no sum function is used at all, e.g. a
simple "a + b".  I don't think that's what the OP wants.



More information about the Python-list mailing list