What does eval() do in Python?
By Fahad Ahammed
- One minute read - 112 wordsI have always loved the fact that The Python interpreter has a number of functions and types built into it. I am going to explore “eval()” in this article.
Syntax: eval(<em>expression</em>[, <em>globals</em>[, <em>locals</em>]])
or
eval(expr, globals=None, locals=None)
So, mainly eval need string as its argument in expression.
<td>
Global namespace to use while executing the source. It must be a dictionary. If not provided then the current global namespace will be used.
</td>
<td>
Local namespace to use while executing the source. It can be any mapping. If omitted, it defaults to <code>globals</code> dictionary.
</td>
You can check globals and locals by global()
and locals().
Example
The output will be –
Isn’t it cool? 🙂