Python Exec in nested Functions Quiz

Quiz Question

Say you have the following module code:

a_global=7defdeepExec():for_closure=3defexecFunction():code="f=2"# Can fool it to nestexeccodeprint"Locals now",locals()print"Closure was taken",for_closureprint"Globals still work",a_globalprint"Added local from code",fexecFunction()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. :-)