Allow eval access to class variables but also to a function from an imported module
I want eval()
to only have access to variables present in a class instance instance
, but also to functions from the module itertools
. How can I do it most elegantly? Without the functions from the module, I do eval(expr, instance.__dict__)
. If I want specific functions, I can add them for example by doing something like eval(expr, instance.__dict__ | {'product': itertools.product})
. But maybe there is a less ad-hoc way of allowing access to the imported modules’ functions?