For the code below:
command = '\'{:.' + str(3) + 'f}\'.format(' + str(12.6543423) + ')'
print(command)
print('{:.3f}'.format(12.6543423))
print(exec(command))
Expected results:
'{:.3f}'.format(12.6543423)
12.654
12.654
Actual results:
'{:.3f}'.format(12.6543423)
12.654
None
Please can someone tell me what I'm doing wrong and how to fix it? I'm both trying to write a number rounding function and trying to understand the exec command.
Mauro Baraldi
6,6042 gold badges35 silver badges48 bronze badges
asked Nov 5, 2014 at 12:24
Jake Levi
3992 gold badges4 silver badges13 bronze badges
-
did you mean eval? have a look here stackoverflow.com/questions/2220699/…Padraic Cunningham– Padraic Cunningham2014年11月05日 12:31:50 +00:00Commented Nov 5, 2014 at 12:31
-
sigh: there's no need to use exec or eval at all...Ned Batchelder– Ned Batchelder2014年11月05日 14:55:39 +00:00Commented Nov 5, 2014 at 14:55
5 Answers 5
Or don't use exec or eval at all. Use the features format offers:
>>> '{:.{}f}'.format(12.6543423, 3)
12.654
answered Nov 5, 2014 at 14:27
Ned Batchelder
378k77 gold badges583 silver badges675 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Jake Levi
Ah, thank you - wasn't aware that you could have curly brackets within curly brackets, probably should have tried that first. Thanks. Even so, I'm keeping the current answer ticked, because I was also curious to learn about eval() and exec().
Ned Batchelder
@JakeLevi learning about eval and exec is good. One of the most important things to learn about them is that they are really really blunt instruments that you almost never need.
Use eval.
>>> eval('\'{:.' + str(3) + 'f}\'.format(' + str(12.6543423) + ')')
'12.654'
answered Nov 5, 2014 at 12:32
Vishnu Upadhyay
5,0611 gold badge17 silver badges24 bronze badges
4 Comments
Mauro Baraldi
Use
ast.literal_eval instead eval Vishnu Upadhyay
@MauroBaraldi Concatenation using + isn't included within that,from the doc The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. and if i am wrong then help me in that. I would love to know.
sebastian
The
+ operation is not a "feature" of eval. Python first concatenates the strings and only then passes a single string to eval. That is of course the case with all function calls (in every programming language)...Ned Batchelder
@MauroBaraldi: did you try
literal_eval? It won't work for this case, because literal_eval won't invoke format for you. It won't call any functions, that's the whole point, to avoid code execution.Use eval() instead of exec() to get the result returned by a expression.
answered Nov 5, 2014 at 12:31
lynn
10.9k1 gold badge48 silver badges81 bronze badges
2 Comments
Mauro Baraldi
Use
ast.literal_eval instead eval Ned Batchelder
@MauroBaraldi: did you try
literal_eval? It won't work for this case, because literal_eval won't invoke format for you. It won't call any functions, that's the whole point, to avoid code execution.or you can use exec
command = '\'{:.' + str(3) + 'f}\'.format(' + str(12.6543423) + ')'
print(command)
mycode = """print('{:.3f}'.format(12.6543423))"""
print('{:.3f}'.format(12.6543423))
exec(mycode)
1 Comment
eivl
it does not. i would also use eval() instead. but it is still usable with exec(). There is in my opinion a lot of confusion regarding exec, also some changes from python 2 and 3.
You are confusing exec and eval:
In [27]: command = 'print'+'('+'\'{:.' + str(3) + 'f}\'.format(' + str(12.6543423) + ')'+')'
In [28]: exec(command)
12.654
answered Nov 5, 2014 at 12:39
Padraic Cunningham
181k30 gold badges264 silver badges327 bronze badges
Comments
lang-py