Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Accessing AST at runtime

Reply
Thread Tools

Accessing AST at runtime

 
 
alessandromoura35@googlemail.com
Guest
Posts: n/a
 
      09-02-2012
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. But I wanted to check here if someone knows one way or another.

Many thanks.
 
Reply With Quote
 
 
 
 
Ramchandra Apte
Guest
Posts: n/a
 
      09-02-2012
On Sunday, 2 September 2012 08:00:59 UTC+5:30, (unknown) 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. But I wanted to check here if someone knows one way or another.
>
>
>
> Many thanks.


You could scan the text for code and then ast.parse() it.
Then you know how...
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      09-02-2012
On 9/1/2012 10:30 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
for-statement in AST hlwangz Java 1 04-26-2006 08:24 PM
for-statement representation in AST hlwangz Java 0 04-26-2006 03:20 AM
Python AST as XML Oleg Paraschenko XML 4 11-04-2004 10:58 AM
cookies between ast and asp.net Arno Seitinger ASP .Net 2 10-28-2004 09:03 AM
AST error r turner Computer Support 1 07-01-2003 04:05 PM



Advertisments