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?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.chainon-marquant.org!rt.uk.eu.org!news-transit.tcx.org.uk!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.04; 'attributes': 0.05; 'advance,': 0.07; 'attribute': 0.07; 'used.': 0.07; 'python': 0.08; '>>>>': 0.09; 'func': 0.09; 'am,': 0.12; "hasn't": 0.13; '"is"': 0.16; 'subject:function': 0.16; '\xa0print': 0.16; '\xa0you': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'jan': 0.19; 'cc:no real name:2**0': 0.20; 'cheers,': 0.20; 'memory': 0.21; 'header:In-Reply-To:1': 0.22; 'changed': 0.23; 'objects,': 0.23; '\xa0if': 0.23; 'received:74.125.82.174': 0.24; 'stack': 0.24; 'code': 0.25; 'function': 0.27; 'all,': 0.28; 'cc:addr:gmail.com': 0.28; 'compare': 0.28; 'message-id:@mail.gmail.com': 0.28; 'print': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'kelly': 0.30; 'subject:each': 0.30; 'subject:?': 0.31; 'objects': 0.32; 'this.': 0.33; 'fri,': 0.34; 'flag': 0.34; 'frame': 0.34; 'received:74.125.82': 0.35; 'thank': 0.35; 'cc:2**1': 0.36; 'thread': 0.37; 'skip:" 10': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; "there's": 0.37; 'think': 0.37; 'using': 0.38; 'e.g.': 0.39; 'should': 0.39; 'subject:how': 0.39; 'your': 0.61; '2012': 0.67; 'identity.': 0.67; 'address,': 0.72; '12:56': 0.84; 'build-in': 0.84; 'case?': 0.84; 'sum': 0.89
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=VSEphoUjCQ82Ft4++eBXXbqcsZmNUb5RLZBcqOijn7A=; b=VoVqDe1EPOBFqkBvPAYGFdVn9Cu4EnF5dvvfWlZe3cnWQxjLZ191G/x2c84R2OgA7b X0+tHYE5w/0ms2eBIreim7dyPyrh0AQpSqIA7Lb/jU/M5OV0PjriwzLUlVTdGeyWsUy5 1n9DdRo3vNdhCf8OdXo4Cfzrjoh93Fjy1fsw0=
MIME-Version 1.0
In-Reply-To <4F0751EA.3020507@davea.name>
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 Fri, 6 Jan 2012 13:37:51 -0700
Subject Re: how to get id(function) for each function in stack?
To d@davea.name
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org, dmitrey <dmitrey15@gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4493.1325882303.27778.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1325882303 news.xs4all.nl 6879 [2001:888:2000:d::a6]:46482
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18618

Show key headers only | 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