New submission from Bikram Singh Mehra <[email protected]>:
Hi Team,
I was parsing python file using AST module but ran into a situation where the
else statement is not found in the parsed data.
---------------------
Module used is: ast
---------------------
In the parsed data I can see "if" followed by "elif" but the "else" part I am
not able to see.
Sample code used:
---------------------
sample_data = """
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
"""
---------------------
tree = ast.parse(sample_data )
The above code provide me below data in ast.dump(tree)
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=[])
While I was traversing through this tree I can't see else in the structure
because it is subpart of orelse i.e. inside elif part.
Doc referred is : https://docs.python.org/3/library/ast.html
Thanks and Best Regards,
Bikram
----------
components: Parser
messages: 393941
nosy: bikrammehra97, lys.nikolaou, pablogsal
priority: normal
severity: normal
status: open
title: Unable to get the else while parsing through AST module
type: enhancement
versions: Python 3.7
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue44177>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com