Closed MartinThoma closed 3 years ago
If(
test=Compare(
left=Name(id='a', ctx=Load()),
ops=[Eq()],
comparators=[Constant(value='foo', kind=None)],
),
body=[
Return(
value=Constant(value='bar', kind=None),
),
],
orelse=[
If(
test=Compare(
left=Name(id='a', ctx=Load()),
ops=[Eq()],
comparators=[Constant(value='bar', kind=None)],
),
body=[
Return(
value=Constant(value='baz', kind=None),
),
],
orelse=[
If(
test=Compare(
left=Name(id='a', ctx=Load()),
ops=[Eq()],
comparators=[Constant(value='boo', kind=None)],
),
body=[
Return(
value=Constant(value='ooh', kind=None),
),
],
orelse=[
Return(
value=Constant(value=42, kind=None),
),
],
),
],
),
],
This style guideline may result in better looking code, but it is inefficient and can even create errors. Each dict value is interpreted whenever the dict is evaluated.
For example:
some_array = [0,1] if x else [0,1,2]
mapping = {
"foo": "bar",
"foo2": long_expensive_function()
"foo3": three_arg_func(some_array[0], some_array[1], some_array[2])
}
return mapping.get("foo",42) # This will still evaluate "foo2", and if x is True, completely crash when evaluating "foo3"
Edit: I created a new issue and referenced this.
Explanation
Example