Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.04; 'processed': 0.05; 'python': 0.09; 'ast': 0.09; 'closest': 0.09; 'it;': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'def': 0.10; 'file,': 0.15; 'bytecode': 0.16; 'discarded.': 0.16; 'mean,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'runtime.': 0.16; 'subject:Accessing': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'module': 0.19; 'code.': 0.20; 'parse': 0.22; 'statement': 0.23; 'this:': 0.23; 'seems': 0.23; 'specifically': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; 'definition': 0.29; 'maybe': 0.29; 'could': 0.32; 'function.': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'possible': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'is.': 0.62; 'information': 0.63; 'more': 0.63; 'within': 0.64; 'all;': 0.84; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Accessing AST at runtime Date: Sun, 02 Sep 2012 02:05:32 -0400 References: <8b41b8a9-b3b8-485a-8e5d-34d28c2d6d66@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: <8b41b8a9-b3b8-485a-8e5d-34d28c2d6d66@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346565950 news.xs4all.nl 6988 [2001:888:2000:d::a6]:60920 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28237 On 9/1/2012 10:30 PM, alessandromoura35@googlemail.com wrote: > Hi, > > I would like to access the AST of a python object at runtime. I mean, > if I have this: > > def f(x): return x*x/2 > > Is there any way to interrogate the object f to find out the AST of > the expression x*x/2 ? Of course if the definition of f were in a > file, I could use the ast module to parse it; but what I want is to > do this from within the code. > > The closest thing I was able to find was f.__code__, and more > specifically f.__code__.co_code, but that is a byte-string which > seems to be the bytecode (?) for the function. > > This may not be possible at all; maybe after the def statement is > processed by the Python interpreter the AST information is discarded. Yes, it is. -- Terry Jan Reedy