What is the difference between locals and globals when using Python's eval()?
Why does it make a difference if variables are passed as globals or as locals to Python's function eval() ? As also described in the documenation , Python will copy __builtins__ to globals, if not given explicitly. But there must be also some other difference which I cannot see. Consider the following example function. It takes a string code and returns a function object. Builtins are not allowed (e.g. abs() ), but all functions from the math package. def make_fn(code): import math ALLOWED_LOCALS = {v:getattr(math, v) for v in filter(lambda x: not x.startswith('_'), dir(math)) } return eval(