This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author bikrammehra97
Recipients bikrammehra97, lys.nikolaou, pablogsal
Date 2021-05-19.09:49:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1621417742.65.0.783924257321.issue44177@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2021-05-19 09:49:02bikrammehra97setrecipients: + bikrammehra97, lys.nikolaou, pablogsal
2021-05-19 09:49:02bikrammehra97setmessageid: <1621417742.65.0.783924257321.issue44177@roundup.psfhosted.org>
2021-05-19 09:49:02bikrammehra97linkissue44177 messages
2021-05-19 09:49:02bikrammehra97create