Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Eval
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Python === In [[Python (language)|Python]], the <code>eval</code> function in its simplest form evaluates a single expression. <code>eval</code> example (interactive shell): <syntaxhighlight lang="pycon"> >>> x = 1 >>> eval('x + 1') 2 >>> eval('x') 1 </syntaxhighlight> The <code>eval</code> function takes two optional arguments, <code>global</code> and <code>locals</code>, which allow the programmer to set up a restricted environment for the evaluation of the expression. The <code>exec</code> statement (or the <code>exec</code> function in Python 3.x) executes statements: <code>exec</code> example (interactive shell): <syntaxhighlight lang="pycon"> >>> x = 1 >>> y = 1 >>> exec "x += 1; y -= 1" >>> x 2 >>> y 0 </syntaxhighlight> The most general form for evaluating statements/expressions is using code objects. Those can be created by invoking the <code>compile()</code> function and by telling it what kind of input it has to compile: an "<code>exec</code>" statement, an "<code>eval</code>" statement or a "<code>single</code>" statement: <code>compile</code> example (interactive shell): <syntaxhighlight lang="pycon"> >>> x = 1 >>> y = 2 >>> eval (compile ("print 'x + y = ', x + y", "compile-sample.py", "single")) x + y = 3 </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)