Is it possible to do the opposite of eval()
in python which treats inputs as literal strings? For example f(Node(2)) -> 'Node(2)'
or if there's some kind of higher-order operator that stops interpreting the input like lisp's quote()
operator.

- 457
- 2
- 6
- 17
-
Can you please provide additional details, such as what exactly you are doing in Python, what kind of input are you passing, because as it is we can't possibly tell if you are confusing between `input` and `raw_input` (as in Python 2), or confusion between [`eval` vs `ast.literal_eval`](https://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval). – metatoaster Jun 23 '20 at 06:26
-
1No, there isn't. What exactly are you trying to accomplish here anyway? – juanpa.arrivillaga Jun 23 '20 at 06:26
-
@juanpa.arrivillaga I'm doing an n-ary tree traversal. While I understand the common way to do it, it came to me that the literal input might be the solution(e.g. `Node(1, [Node(3, [Node(5), Node(6)]), Node(2), Node(4)])` -> [1,3,5,6,2,4]) – Shukai Ni Jun 23 '20 at 06:31
-
@ShukaiNi I think this is a fine and answerable question in principle, and people not familiar with LISP might just not appreciate what quotation does. But you are confusing a couple of things, and the use case you mentions seems to be rather an X-Y-problem. – phipsgabler Jun 23 '20 at 08:51
1 Answers
The answer to does Python have quote
is no, Python does not have quote
. There's no homoiconicity, and no native infrastructure to represent the languages own source code.
Now for what is the opposite of eval
: you seem to be missing some part the picture here. Python's eval
is not the opposite of quote
. There's three types values can be represented as: as unparsed strings, as expressions, and as values (of any type).
quote
and LISP-style "eval
" convert between expressions and values.- Parsing and pretty printing form another pair between expressions and strings.
- Python's actual
eval
goes directly from strings to values, andrepr
goes back (in the sense of printing a parsable value1).
Since SO does not support drawing diagrams:
So if you are looking for the opposite of eval
, it's either trivially repr
, but that might not really help you, or it would be the composition of quote
and pretty priniting, if there were quoting in Python.
This composition with string functions a bit cumbersome, which is why LISPs let the reader deal with parsing once and for all, and from that point on only go between expressions and values. I don't know any language that has such a "quote + print" function, actually (C#'s nameof
comes close, but is restricted to names).
Some caveats about the diagram:
- It does not commute: you have deal with pure values or disregard side effects, for one, and most importantly
quote
is not really a function in some sense, of course. It's a special form. That's whyrepr(x)
is not the same asprettyprint(quote(x))
, in general. - It's not depicting pairs of isomorphisms. Multiple expressions can
eval
to the same value etc.
1That's not always the case in reality, but that's what it's suppsedly there for.

- 20,535
- 4
- 40
- 60
-
Thanks, the diagram helps a lot in clarifying the internal representations. – Shukai Ni Jun 24 '20 at 01:16