[Python-ideas] eval_literal

Jelle Zijlstra jelle.zijlstra at gmail.com
Mon Jun 13 18:16:55 EDT 2016


Yes, it's called ast.literal_eval.
https://docs.python.org/3/library/ast.html#ast.literal_eval

2016-06-13 15:13 GMT-07:00 <jab at math.brown.edu>:

> (Riffing off some discussion in another thread, I had another idea I
> wanted to throw out there.)
>
> Given that Eval Really Is Dangerous[1], has something like this ever
> been considered?:
>
> >>> int(str(42))
> 42
> >>> float(str(42.0))
> 42.0
> >>> bool(str(False))  # :(
> True
> >>> eval_literal('42')
> 42
> >>> eval_literal('42.0')
> 42.0
> >>> eval_literal('False')  # :)
> False
> >>> eval_literal('', default=False)  # shout out to PEP 463 / Michael Selik
> False
>
> i.e. An extremely limited version of eval, possibly just for literals
> or even literal atoms, that would make it safe?
>
>
> [1] http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160613/6b0a1320/attachment.html>


More information about the Python-ideas mailing list