Quiz Question

Say you have the following module code:

a_global = 7


def deepExec():
    for_closure = 3

    def execFunction():
        code = "f=2"

        # Can fool it to nest
        exec code

        print "Locals now", locals()

        print "Closure was taken", for_closure
        print "Globals still work", a_global
        print "Added local from code", f

    execFunction()


deepExec()

Can you overcome the SyntaxError this gives in CPython? Normally exec like this is not allowed for nested functions. Well, think about it, the solution is in the next paragraph.

Solution

The correct answer is that you need to add “in None, None” to the exec and you are fine. The exec is now allowed and behaves as expected. You can see it in the locals, “f” was indeed added to it, the closure value is correct, and the global still works.

It seems the “SyntaxError” tries to avoid such code, but on the other hand, exec is not forbidden when it has parameters, and those imply defaults when they are None.

Now, I had this strange realization when implementing the “exec” behaviour for my Python compiler Nuitka which in its next version (due later this week) will be able to handle this type code as well. :-)

Kay Hayen