Please explain exec function in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
24th Jan 2021, 7:41 AM
∆BH∆Y
∆BH∆Y - avatar
4 Answers
+ 8
Exec simply executes the commands you wrote in the sting as if you wrote it directly in the source code Tip: everything in python has a __doc__ string that tells you everything you need to know regarding that thing
24th Jan 2021, 7:46 AM
Angelo
Angelo - avatar
+ 4
As explained in other questions, eval / exec are considered bad practice because they're generally abused to do a task where they aren't needed, leading to potential security issues and generally bad programming. thanks to Rik Wittkopp sir for explaining the dark side of this function
25th Jan 2021, 5:52 AM
∆BH∆Y
∆BH∆Y - avatar
+ 1
No, exec and eval are not the same eval just evaluates an expression and returns the result So you cannot do things like eval("x=1") or eval("if a: print(b)") (statements) On the other hand you cannot use exec for evaluation: exec("a+b") is possible but useless (exec always returns None) but you can do exec("x=a+b")
24th Jan 2021, 11:01 PM
Angelo
Angelo - avatar
0
Angelo exec() is same as eval() ??
24th Jan 2021, 12:52 PM
Ratnapal Shende
Ratnapal Shende - avatar