How to evaluate Python expressions
This article is about how to evaluate Python expressions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Evaluate Python expressions
We all know the eval function, but do we know the literal_eval function? Maybe a lot of people don't know. You can do this:
Import ast my_list = ast.literal_eval (expr)
Instead of doing the following:
Expr = "[1,2,3]" my_list = eval (expr)
I believe this is the first time this form has been seen for most people, but in fact it has been around in Python for a long time.
Thank you for reading! This is the end of the article on "how to evaluate Python expressions". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!