PLEASE NOTE: This is NOT about the use of eval(), it is about the potential quality (or lack thereof) of a book it is used and taught in. SO already has countless threads ab
Yes eval
should be spelled evil
instead to warn people about this ;) You should try and never use it unless you absolutely have to. In this case it's intuitive to use int()
instead, it's even much more readable! Also if you really had to you could use ast.literal_eval
(it only evaluates literals as the name implies, so it won't allow the user to run malicious code) which is actually safe, but there is no need for this and there is no need for eval()
in this case.