eval(expression, globals=None, locals=None) |
Returns test |
expression | ?? |
globals | |
locals |
REMARKS |
* No prefix required - Core * The arguments are a string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can be any mapping object. * The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals and locals dictionaries as global and local namespace. * If the globals dictionary is present and does not contain a value for the key __builtins__, a reference to the dictionary of the built-in module builtins is inserted under that key before expression is parsed. That way you can control what builtins are available to the executed code by inserting your own __builtins__ dictionary into globals before passing it to eval(). If the locals dictionary is omitted it defaults to the globals dictionary. * If both dictionaries are omitted, the expression is executed with the globals and locals in the environment where eval() is called. Note, eval() does not have access to the nested scopes (non-locals) in the enclosing environment. * The return value is the result of the evaluated expression. Syntax errors are reported as exceptions. * This function can also be used to execute arbitrary code objects (such as those created by compile()). In this case, pass a code object instead of a string. If the code object has been compiled with 'exec' as the mode argument, eval()'s return value will be None. * Hints: dynamic execution of statements is supported by the exec() function. The globals() and locals() functions return the current global and local dictionary, respectively, which may be useful to pass around for use by eval() or exec(). * If the given source is a string, then leading and trailing spaces and tabs are stripped. |
>>> x = 1
>>> eval('x+1')
2
© 2025 Better Solutions Limited. All Rights Reserved. © 2025 Better Solutions Limited Top