[IPython-dev] Musings: syntax for high-level expression of parallel (and other) execution control

Fernando Perez fperez.net@gmail....
Tue Sep 8 23:30:08 CDT 2009

On Tue, Sep 8, 2009 at 8:13 PM, Brian Granger<ellisonbg.net@gmail.com> wrote:
> Another link that is interesting when you start to think about bytecode
> transformations:
> http://www.voidspace.org.uk/python/articles/code_blocks.shtml

Ah, very nice!  Thanks for that link, this thread is really turning
out to be very useful and informative, Michael Foord's is a very nice

It's worth noting that Michael's clever bytecode hack isn't needed, as
I mentioned before, in python 3.x because what he's doing is precisely
what the new 'nonlocal' keyword provides.  But for 2.x either we use
some of the approaches I mentioned above, or this kind of trickery.

Michael's hack has unfortunately some limitations, as best I can see:
you can't capture enclosing scope easily (without much deeper
modifications of the bytecode, which are doable but which I do NOT
want to get into).  If you try to put a closure in the block, python
refuses the bare 'exec':

In [12]: run simple
SyntaxError: unqualified exec is not allowed in function 'simple' it
contains a nested function with free variables (simple.py, line 39)

WARNING: Failure executing file: <simple.py>

where I'd written:

def simple(n):
    s = 0.0

    def block():
        for i in range(n):
            s += i**2
    exec AnonymousCodeBlock(block)
    return s

But I think we have a reasonable path forward for actually useful tool
building (aside from neat machinations and hackery):

- in python 2.x, all of this works, but some slightly ugly solutions
must be used to return information from the wrapped functions.

- in python 3.x, using nonlocal solves the above.

- if all of this leads to useful libraries, it's plausible to consider
it for later inclusion in the language with better syntactic support,
once we understand more angles of the problem and have proven its



More information about the IPython-dev mailing list