Python Forum
python ast if-elif confusion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python ast if-elif confusion
#1
Hi,

I'm working with the python ast and have a question regarding difference between how different else conditions are handled. I'm finding that an elif statement and else: followed by a subsequent are stored in the same manner as part of the syntax tree.

Original Code:
a = 0
b = 0
c = 0
if a == 0:
    b = 1  
elif a == 1:
    b = 2
else:
  if c == 0:
      b = 3
  else:
      b = 4
I'm assuming this is the case because these statements are logically equivalent, so when the AST is actually processed by the python interpreter it views them as the same.
The reason I'm asking this is I'm building a code translator which takes simple procedural statements and translates them to a variety of languages, so I'd like to get the resulting code as close to the python source as possible. For example, if I were to re-translate the python AST back into python it would then look like:

XLT'd code
a = 0
b = 0
c = 0
if a == 0:
    b = 1  
elif a == 1:
    b = 2
elif c == 0:
     b = 3
else:
     b = 4
Note that the last else branch got modified in the above. Looking at the syntax tree for the original code, the if statemnet looks something like this:

Output:
If (a == 0) Body -> Assign(b = 1) Orelse -> If (a == 1) -> Body -> Assign(b = 2) -> Orelse -> If (c == 0) -> Body -> Assign(b = 3) -> Orelse -> Assign(b = 4)
Anyway, the root of my issue is that elif and else->if statements look the same in the sytnax tree which results in my translation code handling them in the same manner and translating them to code which isn't exactly the same as the source.

Basically I'm handling it if I see only 1 'If' node within an orelse statement, I treat it as an 'elif' statement. If there is more than one statement (assign or if), then it writes a 'if' statement.

Am I missing something from the syntax tree which would differentiate these statements?

Note I've been using https://python-ast-explorer.com/ to view the AST for the code.

Thanks!
Reply
#2
In the first code, if the value of a is not 0 or 1 then it moves on to check c, and b gets the following assigned value
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
As far as I can see the first code has the same result as the second code.
Reply
#4
(Apr-18-2020, 12:11 PM)ibreeden Wrote: As far as I can see the first code has the same result as the second code.
Yes, you are right. My bad
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  simple if elif conditions in Python Chandan 2 2,073 Jan-08-2020, 02:36 PM
Last Post: DeaD_EyE
  Python coding mutiple elif's metro17 1 1,776 Aug-02-2019, 10:27 AM
Last Post: metulburr
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,246 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  Python and bash command confusion MuntyScruntfundle 0 2,059 Oct-11-2018, 04:52 PM
Last Post: MuntyScruntfundle
  python indentation confusion hello_its_me 5 4,381 Sep-02-2017, 12:04 PM
Last Post: hello_its_me
  File access confusion python windows PickyBiker 0 3,366 May-08-2017, 05:36 PM
Last Post: PickyBiker

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020