Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4620970
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:38:00+00:00 2026-05-22T02:38:00+00:00

I am working on Python 2.6.5. Given a Abstract Syntax Tree, I want to

  • 0

I am working on Python 2.6.5.

Given a Abstract Syntax Tree, I want to obtain its children.

Most StackOverflow posts discuss ast.NodeVisitor and the methods defined in it: visit(), generic_visit().
However, visit() and generic_visit() do not give the children, rather they directly apply the function recursively on them.

Can someone please write a short code or so to demonstrate it?
Does there exist a predefined function in python library for the same?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:38:01+00:00Added an answer on May 22, 2026 at 2:38 am

    The attaributes containing the node’s children depend on the type of syntax the node represents. Every node class also has a special _fields attribute, that lists the attribute names for the child nodes that class has. For instance,

    >>> ast.parse('5+a')
    <_ast.Module object at 0x02C1F730>
    >>> ast.parse('5+a').body
    [<_ast.Expr object at 0x02C1FF50>]
    >>> ast.parse('5+a').body[0]
    <_ast.Expr object at 0x02C1FBF0>
    >>> ast.parse('5+a').body[0]._fields
    ('value',)
    >>> ast.parse('5+a').body[0].value
    <_ast.BinOp object at 0x02C1FF90>
    >>> ast.parse('5+a').body[0].value._fields
    ('left', 'op', 'right')
    >>> ast.parse('5+a').body[0].value.left
    <_ast.Num object at 0x02C1FB70>
    

    and so on.

    Edit, to clarify what’s going on

    Before going any further, take a glance at the CPython Abstract Grammar

    Consider:

    >>> type(ast.parse('5+a'))
    <class '_ast.Module'>
    

    In fact, if you look at the grammar, the first production rule is for Module. It appears to take a sequence of statements, as an argument called body.

    >>> ast.parse('5+a')._fields
    ('body',)
    >>> ast.parse('5+a').body
    [<_ast.Expr object at 0x02E965B0>]
    

    The _fields attribute of the AST is just “body”, and the body attribute is a sequence of AST nodes. Back to the grammar, looking in the production rules for stmt, we see that Expr takes a single expr, named value

    >>> ast.parse('5+a').body[0].value
    <_ast.BinOp object at 0x02E96330>
    

    If we look up the definition for BinOp, we see that it takes 3 different arguments, left, op and right. You should be able to proceed from there, I hope.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm working in the Python shell and I'm given a function f
Working in Python, given a list of N sets of integers from the range
Working with python interactively, it's sometimes necessary to display a result which is some
I've been working with python for a while now and am just starting to
I have recently been working with Python using Komodo Edit and other simpler editors
I'm working on a Python library that interfaces with a web service API. Like
I'm working on a Python package named lehmer that includes a bunch of extension
I am working on a web application using Python (Django) and would like to
I'm working on a macro system for Python ( as discussed here ) and
I'm a C/Python programmer in C++ land working with the STL for the first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.