Closed MartinThoma closed 2 years ago
$ astpretty --no-show-offsets /dev/stdin <<< `cat example.py`
Module(
body=[
If(
test=Compare(
left=Name(id='E', ctx=Load()),
ops=[Eq()],
comparators=[Constant(value=0, kind=None)],
),
body=[
Assign(
targets=[Name(id='M', ctx=Store())],
value=Constant(value=3, kind=None),
type_comment=None,
),
],
orelse=[
If(
test=Compare(
left=Name(id='E', ctx=Load()),
ops=[Eq()],
comparators=[Constant(value=1, kind=None)],
),
body=[
Assign(
targets=[Name(id='M', ctx=Store())],
value=Constant(value=2, kind=None),
type_comment=None,
),
],
orelse=[
Assign(
targets=[Name(id='M', ctx=Store())],
value=Constant(value=0.5, kind=None),
type_comment=None,
),
],
),
],
),
],
type_ignores=[],
)
Uuuuh, damn: What this proposes is technically correct, but for sure harder to understand.
if E == 0:
M = 3
elif E == 1:
M = 2
else:
M = 0.5
if E == 0:
M = 3
else:
M = 2 if E == 1 else 0.5
If there were a lot more values, I would go with the dictionary pattern:
M = {0: 3, 1: 2}.get(E, 0.5)
But here, I think nothing should be changed.
Desired change
Explanation
The shown error message is wrong
Example
Gives this message: