Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28229 > unrolled thread

Accessing AST at runtime

Started byalessandromoura35@googlemail.com
First post2012-09-01 19:30 -0700
Last post2012-09-02 02:05 -0400
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Accessing AST at runtime alessandromoura35@googlemail.com - 2012-09-01 19:30 -0700
    Re: Accessing AST at runtime Ramchandra Apte <maniandram01@gmail.com> - 2012-09-01 19:48 -0700
    Re: Accessing AST at runtime Terry Reedy <tjreedy@udel.edu> - 2012-09-02 02:05 -0400

#28229 — Accessing AST at runtime

Fromalessandromoura35@googlemail.com
Date2012-09-01 19:30 -0700
SubjectAccessing AST at runtime
Message-ID<8b41b8a9-b3b8-485a-8e5d-34d28c2d6d66@googlegroups.com>
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.

[toc] | [next] | [standalone]


#28230

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-01 19:48 -0700
Message-ID<90dd585c-360b-461d-b306-493ae170974a@googlegroups.com>
In reply to#28229
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...

[toc] | [prev] | [next] | [standalone]


#28237

FromTerry Reedy <tjreedy@udel.edu>
Date2012-09-02 02:05 -0400
Message-ID<mailman.57.1346565950.27098.python-list@python.org>
In reply to#28229
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web