Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The attaributes containing the node's children depend on the type of syntax the node represents. Every node class also has a special <code>_fields</code> attribute, that lists the attribute names for the child nodes that class has. For instance, </p> <pre><code>&gt;&gt;&gt; ast.parse('5+a') &lt;_ast.Module object at 0x02C1F730&gt; &gt;&gt;&gt; ast.parse('5+a').body [&lt;_ast.Expr object at 0x02C1FF50&gt;] &gt;&gt;&gt; ast.parse('5+a').body[0] &lt;_ast.Expr object at 0x02C1FBF0&gt; &gt;&gt;&gt; ast.parse('5+a').body[0]._fields ('value',) &gt;&gt;&gt; ast.parse('5+a').body[0].value &lt;_ast.BinOp object at 0x02C1FF90&gt; &gt;&gt;&gt; ast.parse('5+a').body[0].value._fields ('left', 'op', 'right') &gt;&gt;&gt; ast.parse('5+a').body[0].value.left &lt;_ast.Num object at 0x02C1FB70&gt; </code></pre> <p>and so on.</p> <p><em>Edit, to clarify what's going on</em></p> <p>Before going any further, take a glance at the <a href="http://docs.python.org/library/ast.html#abstract-grammar" rel="nofollow">CPython Abstract Grammar</a></p> <p>Consider: </p> <pre><code>&gt;&gt;&gt; type(ast.parse('5+a')) &lt;class '_ast.Module'&gt; </code></pre> <p>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. </p> <pre><code>&gt;&gt;&gt; ast.parse('5+a')._fields ('body',) &gt;&gt;&gt; ast.parse('5+a').body [&lt;_ast.Expr object at 0x02E965B0&gt;] </code></pre> <p>The <code>_fields</code> 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 <code>stmt</code>, we see that <code>Expr</code> takes a single expr, named <code>value</code></p> <pre><code>&gt;&gt;&gt; ast.parse('5+a').body[0].value &lt;_ast.BinOp object at 0x02E96330&gt; </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload