on perhaps unloading modules?

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Aug 20 20:06:23 EDT 2021


On 21/08/21 6:15 am, Hope Rouselle wrote:
>>>> code()
> 'def p():\n  import math\n  return math.e\n'
>>>> exec(code())
>>>> p
> <function p at 0x0113F5D0>
>>>> p()
> 2.718281828459045

Note that this pollutes the globals of the module that you're calling
exec() from. For better isolation you can pass in an explicit globals
dict:

g = {}
exec(code(), g)
g['p']()

-- 
Greg



More information about the Python-list mailing list