You can use any number of statements here. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What's the difference between eval, exec, and compile? Ask Question. Asked 11 years, 9 months ago. Active 1 year ago. Viewed k times. Add a comment. Active Oldest Votes. The short answer, or TL;DR Basically, eval is used to eval uate a single dynamically generated Python expression, and exec is used to exec ute dynamically generated Python code only for its side effects.
A source fragment containing 2 top-level statements is an error for the 'single' , except in Python 2 there is a bug that sometimes allows multiple toplevel statements in the code; only the first is compiled; the rest are ignored: In Python 2. Syntax differences of exec between Python 2 and Python 3 One of the major differences in Python 2 is that exec is a statement and eval is a built-in function both are built-in functions in Python 3.
Antti Haapala Antti Haapala k 21 21 gold badges silver badges bronze badges. Was [i for i in globals. If it was an expression, why can't I use it with as decorator? Not anything that can be put on the right side of an assignment and still compile is an expression. It might be worthwhile to also mention ast. I use it a lot to evaluate a string which may represent a bool , int , float , Show 1 more comment. Example: exec 'print 5 ' prints 5.
The modes are as follows: compile string, '', 'eval' returns the code object that would have been executed had you done eval string. Max Shawabkeh Max Shawabkeh In Python 3, exec now is in fact a function.
Since as you point out , exec is a statement in the version you were targeting, it's deceptive to include those parens, and if you try to use the in globals, locals , also buggy. MikeGraham exec supports parentheses and function like invocation in Python 2. Another statement-turned-function is print ; compare the result of print 1, 2, 3 in python 2 and 3. Please read the bottom of my answer here and be surprised. Show 4 more comments. Wu Li Wu Li 5 5 silver badges 5 5 bronze badges.
The second paragraph is such a simplification that it almost becomes a lie, an expression can very much do something if it includes a function call. Also, the command following exec didn't run, since the shell was replaced. In other words eval foo bar is the same as just foo bar. But variables will be expanded before executing, so we can execute commands saved in shell variables:. It will not create a child process, so the variable is set in the current shell.
Or we could have a command that outputs shell commands. What is the difference between isdigit, isnumeric and isdecimal in python?
Lowercase in Python You can simply the built-in function in What is the difference between range and xrange functions in Python 2. What is the difference between Python and IPython? Welcome back to the World's most active Tech Community! Password must have. Please enter a valid emailid. Forgot Password? Subscribe to our Newsletter, and get personalized recommendations. Sign up with Google Signup with Facebook Already have an account?
Email me at this address if a comment is added after mine: Email me if a comment is added after mine. Privacy: Your email address will only be used for sending these notifications.
Add comment Cancel.
0コメント