Path: csiph.com!x330-a1.tempe.blueboxinc.net!hal.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'distinct': 0.05; 'subject:position': 0.05; 'attribute': 0.07; 'python': 0.08; 'attribute.': 0.09; 'function:': 0.09; 'iterate': 0.09; 'stack,': 0.09; 'subject:string': 0.09; 'tuple.': 0.09; 'am,': 0.12; 'def': 0.13; 'folks': 0.15; '"is"': 0.16; 'bytecode': 0.16; 'frames': 0.16; 'lambda': 0.16; 'problem).': 0.16; 'subject:function': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '(which': 0.19; 'cc:no real name:2**0': 0.20; 'cheers,': 0.20; 'suggest': 0.20; 'trying': 0.21; 'maybe': 0.21; 'dec': 0.22; 'header:In-Reply- To:1': 0.22; 'appear': 0.23; 'objects,': 0.23; 'string': 0.24; 'cc:2**0': 0.24; 'stack': 0.24; 'code': 0.25; 'helpful': 0.26; 'testing': 0.26; 'function': 0.27; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'class': 0.29; 'generally': 0.30; 'ctypes': 0.30; 'modules,': 0.30; 'objects.': 0.30; 'subject:?': 0.31; 'does': 0.32; 'list': 0.32; 'actual': 0.32; 'objects': 0.32; "won't": 0.33; 'rather': 0.33; 'instead': 0.33; 'there': 0.33; 'object': 0.33; 'fri,': 0.34; 'probably': 0.34; 'it.': 0.34; 'calling': 0.34; 'anything': 0.34; 'frame': 0.34; 'stores': 0.34; 'received:74.125.82': 0.35; 'thank': 0.35; 'something': 0.35; 'subject:How': 0.35; 'example,': 0.37; 'comparing': 0.37; 'two': 0.37; 'but': 0.37; 'except': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'getting': 0.38; 'subject:from': 0.38; 'e.g.': 0.39; 'represent': 0.39; 'should': 0.39; 'one,': 0.40; 'more': 0.61; '2011': 0.61; 'you.': 0.63; 'subject:name': 0.67; 'details.': 0.72; '30,': 0.74; '11:43': 0.84; 'hole': 0.84; 'other?': 0.84; 'sum': 0.89; 'avoided.': 0.91 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; bh=g/H063PKgfhEkEd5FwEhBZfHAIP8Rfn8PSYHIaLjsUE=; b=kwk0NLskr1+KyyzuujPUkc6kgx9n3WOvAlPGWjy20JK16tDLpdleckhf9SPzgSmyyE B5kURhsDSF4WQ0CZUMXo8Yd2K24dRZylh71AWuCTotcFZc1bPFemWWyjvy1G+wp7cIaQ ialaUALu3K56eOw9zyZm64hKvBa7WB+Sq2xDI= MIME-Version: 1.0 In-Reply-To: References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> From: Ian Kelly Date: Fri, 30 Dec 2011 14:48:49 -0700 Subject: Re: How to get function string name from i-th stack position? To: dmitrey Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325281766 news.xs4all.nl 6926 [2001:888:2000:d::a6]:50127 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18232 On Fri, Dec 30, 2011 at 11:43 AM, dmitrey wrote: > Thank you. And what should I do to get function by itself instead of > its string name, e.g. I want to know does this function is my_func or > any other? For example, I would like to check is this function Python > sum(), or maybe numpy.sum(), or anything else? The Python stack only includes Python code objects. Built-ins like sum won't appear in it because they're basically C functions and don't have associated code objects. If you really want to see them, you could probably do something with ctypes to inspect the C stack, but I don't recommend it. You can get the Python code objects from the stack by calling inspect.stack(), which includes each frame object currently on the stack as the first member of each tuple. E.g.: frames = map(operator.itemgetter(0), inspect.stack()) Each frame has an f_code attribute that stores the code object associated with that frame. Getting the actual function from the code object is tricky, for two reasons. One, not all code objects represent functions. There are also code objects for modules, class definitions, and probably other thing as well. Two, code objects don't have associated functions. The relationship is the reverse: functions have associated code objects. You would have to iterate over each function that you're interested in, looking for one with a func_code attribute that "is" the frame's f_code attribute. In any case, testing function identity is a rather large rabbit hole that is best avoided. These are mathematically the same function: def plus1(value): return value + 1 plus_one = lambda x: x + 1 But they are two distinct function objects, and there is no way programmatically to determine that they are the same function except by comparing the bytecode (which won't work generally because of the halting problem). What is it that you're trying to do? Perhaps the helpful folks on the list will be able to suggest a better solution if you can provide more details. Cheers, Ian