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: 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> <9f8a8fd4-3541-4f9b-a887-3d10524de8f1@t30g2000vbx.googlegroups.com> <4F0751EA.3020507@davea.name> From: Ian Kelly 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 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Fri, Jan 6, 2012 at 12:56 PM, Dave Angel wrote: > On 01/06/2012 02:29 PM, dmitrey wrote: >> >> On Jan 6, 8:28 pm, Ian Kelly =A0wrote: >>> >>> On Fri, Jan 6, 2012 at 11:02 AM, dmitrey =A0wrote: >>>> >>>> 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. =A0The >>> stack contains code objects, not functions. =A0You 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. =A0Just use the "is" >>> operator to check identity. >>> >>> for frame_tuple in inspect.stack(): >>> =A0 =A0 frame =3D frame_tuple[0] >>> =A0 =A0 if frame.f_code is some_function.func_code: >>> =A0 =A0 =A0 =A0 print("Found it!") >>> >>> Cheers, >>> Ian >> >> Python build-in function sum() has no attribute func_code, what should >> I do in the case? >> D. > > flag =3D False > > > for frame_tuple in inspect.stack(): > =A0 =A0frame =3D frame_tuple[0] > =A0 =A0if frame.f_code is some_function.func_code: > =A0 =A0 =A0 =A0flag =3D True > > if !flag: > =A0 =A0print "FuncDesigner.sum() not used. =A0Change." 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.