When running a Python REPL, top-level statements sent to the console get auto-printed. For example, with:
>>> x = 1
>>> x
1
>>> |
The second line evaluating x
caused the value to be 'auto-printed' However, we don't get the same behavior when x
is evaluated in other contexts -- for example, if we use exec
:
>>> exec('x')
>>> |
Similarly, with eval()
:
>>> code = compile('x = 1; x', '<string>', 'exec')
>>> eval(code)
>>> |
Is there a way to ask Python to auto-print statements run in this way, as it does at the top level of the REPL?
While this question describes Python code directly, I'd also love to know whether it's possible to accomplish this using the Python C API (e.g. with PyRun_SimpleStringFlags()
).