Function Details: eval
Description
Evaluates a Python expression as a string and return the result.
Extended Description
The eval() function evaluates a Python expression (as a string) and returns the result. It can be used to dynamically execute Python code. However, it should be used with caution, especially with untrusted input, as it can execute arbitrary Python code. The optional globals and locals arguments can be used to specify the environment in which the code is executed.
Exceptions
- SyntaxError: If the string contains invalid Python syntax
- NameError: If a name used in the expression is not defined
- TypeError: If an operation in the expression is applied to an object of inappropriate type
Read More about eval from Python Documentation
Function Signature
eval(expression: str, globals: Optional[dict] = None, locals: Optional[dict] = None) -> Any
Module: builtins
Parameters
expression: A string containing a Python expression to be evaluated. globals: Optional dictionary to specify global variables available to the code being evaluated. locals: Optional dictionary to specify local variables available to the code being evaluated.
Parameter List
- expression: str
- globals: Optional[dict]
- locals: Optional[dict]
Return
Returns the result of evaluating the given expression.
Return Type
Any
Output
Explanation
This example shows basic usage of eval() with different types of expressions.