This is not really an answer but an opinion (politically incorrect).
Exchange formats
Both json, yaml, xml, ... modules contain functions that recognize the
respective formats (languages) and create a semantic representation
parser : STR -> objectoPythonEspecífico
and serialize prittyprint : objectoPythonEspecífico -> STR
typically used for exchange of constant structured between different
contexts (different sessions, processes, languages, machines, platforms).
Ast.literal_eval
The ast.literal_eval
is something similar in which the format is
a small subset of the Python language including
- constants (str, numbers, tuples, lists, dictionaries)
- may use a set of "peaceful operators".
For example, it does not allow expressions containing:
(1) variables and functions,
(2) indexing of tuples, lists, dictionaries.
The ast.literal_eval
is non-reflective.
val
The eval(expressão)
, exec(strcodigo)
, are much more powerful:
allows all the syntax
of Python expressions, allow
reflexivity: we can access and define new variables, functions, etc.
(in)Security
Fortunately you can do dangerous things with eval
, with Python,
with system()
, with the bash
, with any powerful tool.
Of course, miracles can also be performed.
The eval(str)
has to be used carefully when the str
has origins
uncontrolled and potentially adverse. If the str
is somehow
dependent on user interaction, the situation is as dangerous as
the user is dangerous.
eval(rawinput())
is precisely what we are running when we run
the Python interpreter -- and it never kept anyone awake. If we do
something analogous with a web application: ... it will go wrong!
I see no harm in indiscriminately using Eval in my activities.
I defined a calculator that, like the Python interpreter, uses something
print(pp(eval(retoca(rawinput("?"))))
and that allows me fantastic things and also format the disk and everything else!
No Soen: Using python’s Eval() vs. Ast.literal_eval()?
– Woss
E. obviously, it’s always worth quoting: Eval is either good or bad?
– Woss
Anderson: Please correct my answer. Eval is good or bad guy? -- Eval is more Rambo.
– JJoao
@Jjoao did not understand your comment
– Woss