Try it here
Subscribe
Python builtin function - eval

eval in Python

eval_in_python

eval(expression[, globals[, locals]])

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. This means that expression normally has full access to the standard builtins module and restricted environments are propagated. 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 eval() method parses the expression passed to it and runs python expression(code) within the program.

  • expression: this string is parsed and evaluated as a Python expression
  • globals (optional): a dictionary to specify the available global methods and variables.
  • locals (optional): another dictionary to specify the available local methods and variables.
Ex.
>>> x = 1
>>> eval('x + 1')
2
>>> 

Writer profile pic

Devbarma on Sep 03, 2020 at 11:09 am


This article is contributed by Devbarma. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

×

Forgot Password

Please enter your email address below and we will send you information to change your password.