Message393947
Formatting the result, I get:
Module(body=[If(test=Compare(left=Name(id='num', ctx=Load()),
ops=[Gt()],
comparators=[Constant(value=0, kind=None)]),
body=[Expr(value=Call(func=Name(id='print', ctx=Load()),
args=[Constant(value='Positive number',
kind=None)],
keywords=[]))],
orelse=[If(test=Compare(left=Name(id='num', ctx=Load()),
ops=[Eq()],
comparators=[Constant(value=0, kind=None)]),
body=[Expr(value=Call(func=Name(id='print', ctx=Load()),
args=[Constant(value='Zero', kind=None)],
keywords=[]))],
orelse=[Expr(value=Call(func=Name(id='print', ctx=Load()),
args=[Constant(value='Negative number', kind=None)],
keywords=[]))])])],
type_ignores=[])
You'll have to extract the "else" part by walking the tree yourself.
> I was parsing python file using AST module but ran into a situation where the else statement is not found in the parsed data.
It is in the parsed data, though. Just not directly in the "If" node. |
|
Date |
User |
Action |
Args |
2021-05-19 11:31:16 | eric.smith | set | recipients:
+ eric.smith, lys.nikolaou, pablogsal, bikrammehra97 |
2021-05-19 11:31:16 | eric.smith | set | messageid: <1621423876.15.0.179552341486.issue44177@roundup.psfhosted.org> |
2021-05-19 11:31:16 | eric.smith | link | issue44177 messages |
2021-05-19 11:31:16 | eric.smith | create | |
|