Using python's eval() vs. ast.literal_eval()?

前端 未结 6 1489
情深已故
情深已故 2020-11-21 06:39

I have a situation with some code where eval() came up as a possible solution. Now I have never had to use eval() before but, I have come across p

6条回答
  •  天命终不由人
    2020-11-21 07:24

    datamap = eval(input('Provide some data here: ')) means that you actually evaluate the code before you deem it to be unsafe or not. It evaluates the code as soon as the function is called. See also the dangers of eval.

    ast.literal_eval raises an exception if the input isn't a valid Python datatype, so the code won't be executed if it's not.

    Use ast.literal_eval whenever you need eval. You shouldn't usually evaluate literal Python statements.

提交回复
热议问题