Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18618

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

References <1002d4cd-6cfe-4b79-917f-361a06ffd215@a11g2000vbz.googlegroups.com> <mailman.4490.1325874536.27778.python-list@python.org> <9f8a8fd4-3541-4f9b-a887-3d10524de8f1@t30g2000vbx.googlegroups.com> <4F0751EA.3020507@davea.name>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-01-06 13:37 -0700
Subject Re: how to get id(function) for each function in stack?
Newsgroups comp.lang.python
Message-ID <mailman.4493.1325882303.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jan 6, 2012 at 12:56 PM, Dave Angel <d@davea.name> wrote:
> On 01/06/2012 02:29 PM, dmitrey wrote:
>>
>> On Jan 6, 8:28 pm, Ian Kelly<ian.g.ke...@gmail.com>  wrote:
>>>
>>> On Fri, Jan 6, 2012 at 11:02 AM, dmitrey<dmitre...@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to get id(function) for each function in stack? dmitrey <dmitrey15@gmail.com> - 2012-01-06 10:02 -0800
  Re: how to get id(function) for each function in stack? Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-06 11:28 -0700
    Re: how to get id(function) for each function in stack? dmitrey <dmitrey15@gmail.com> - 2012-01-06 11:29 -0800
      Re: how to get id(function) for each function in stack? Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-06 12:50 -0700
      Re: how to get id(function) for each function in stack? Dave Angel <d@davea.name> - 2012-01-06 14:56 -0500
      Re: how to get id(function) for each function in stack? Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-06 13:37 -0700
      Re: how to get id(function) for each function in stack? Lie Ryan <lie.1296@gmail.com> - 2012-01-07 11:17 +1100
      Re: how to get id(function) for each function in stack? Robert Kern <robert.kern@gmail.com> - 2012-01-07 10:39 +0000
  Re: how to get id(function) for each function in stack? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-07 00:54 +0000

csiph-web