[Python-ideas] If branch merging

Chris Angelico rosuav at gmail.com
Wed Jun 10 02:54:55 CEST 2015

On Wed, Jun 10, 2015 at 10:20 AM, Andrew Barnert via Python-ideas
<python-ideas at python.org> wrote:
> Now, for implementation: any statement that contains an as expression anywhere is compiled to a function definition and a call to that function. The only trick is that any free variables have to be compiled as nonlocals in the inner function and as captured locals in the real function. (This trick doesn't have to apply to lambdas or comprehensions, because they can't have assignment statements inside them, but a while statement can.) I believe this scales to nested statements with as-bindings, and to as-bindings inside explicit local functions and vice-versa.

I'd actually rather see this implemented the other way around: instead
of turning this into a function call, actually have a real concept of
nested scoping. Nested functions imply changes to tracebacks and such,
which scoping doesn't require.

How hard would it be to hack the bytecode compiler to treat two names
as distinct despite appearing the same? Example:

def f(x):
    e = 2.718281828
        return e/x
    except ZeroDivisionError as e:
        raise ContrivedCodeException from e

Currently, f.__code__.co_varnames is ('x', 'e'), and all the
references to e are working with slot 1; imagine if, instead,
co_varnames were ('x', 'e', 'e') and the last two lines used slot 2
instead. Then the final act of the except clause would be to unbind
its local name e (slot 2), and then any code after the except block
would use slot 1 for e, and the original value would "reappear".

The only place that would need to "know" about the stack of scopes is
the compilation step; everything after that just uses the slots. Is
this feasible?


More information about the Python-ideas mailing list