[Python-ideas] If branch merging

Nick Coghlan ncoghlan at gmail.com
Sun Jun 7 08:15:14 CEST 2015

On 7 June 2015 at 15:50, Bruce Leban <bruce at leban.us> wrote:
> This is a generic example but I've written code like this many times and
> there is no simple way to say that all the foo.x values don't need to be
> computed more than once.

This is one of the key powers of JIT compilers like PyPy and Numba -
they can detect that a calculation is repeated and avoid repeating it
when the compiler knows the input values haven't changed.

There is no way any syntax addition can compete for clarity with using
existing already clear syntax and speeding its execution up

> Here it is rewritten to avoid recomputation:
> foo_a = foo.a
> if foo_a == 0:
>    ...
> else:
>     foo_b = foo.b
>     if foo_a == 1 and foo_b == 0:
>         ...
>     else:
>         foo_c = foo.c
>         if foo_a >= 1 and foo_b >= 0 and foo_c = 0:
>             ...
>         else:
>             ...
> Much harder to follow the logic.

It's hard to reason about whether or not logic is difficult to follow
when using metasyntactic variables, as they're never self-documenting.
There's also the fact that this *specific* example is why having
expensive-to-calculate values accessed as attributes without some form
of caching is a bad idea - it encourages folks to make their code
harder to read too early in the development process.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list