Accessing AST at runtime

Discussion in 'Python' started by alessandromoura35@googlemail.com, Sep 2, 2012.

  1. Guest

    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.
     
    , Sep 2, 2012
    #1
    1. Advertising

  2. 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...
     
    Ramchandra Apte, Sep 2, 2012
    #2
    1. Advertising

  3. Terry Reedy Guest

    On 9/1/2012 10:30 PM, 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
     
    Terry Reedy, Sep 2, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arno Seitinger

    cookies between ast and asp.net

    Arno Seitinger, Oct 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    500
    Saravana
    Oct 28, 2004
  2. hlwangz
    Replies:
    0
    Views:
    382
    hlwangz
    Apr 26, 2006
  3. hlwangz

    for-statement in AST

    hlwangz, Apr 26, 2006, in forum: Java
    Replies:
    1
    Views:
    377
    Oliver Wong
    Apr 26, 2006
  4. Oleg Paraschenko

    Python AST as XML

    Oleg Paraschenko, Oct 28, 2004, in forum: XML
    Replies:
    4
    Views:
    479
    Dimitre Novatchev
    Nov 4, 2004
  5. news.tkdsoftware.com

    Computing AST

    news.tkdsoftware.com, Sep 24, 2004, in forum: C++
    Replies:
    3
    Views:
    382
    Thomas Matthews
    Sep 27, 2004
Loading...

Share This Page