how to get id(function) for each function in stack?
Dave Angel
d at davea.name
Fri Jan 6 14:56:26 EST 2012
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."
--
DaveA
More information about the Python-list
mailing list