Interpreter-style Output In Python 3 (maybe About Sys.displayhook?)
I'm making a little toy command window with Tk, and currently trying to make it copy some interpreter behavior. I'd never scrutinized the interpreter before, but it's decisions on
Solution 1:
The interpreter prints out repr(result)
only if result is not None
.
There are no "implied print
s" like you thought.
3 + 4
results to7
, sorepr(7)
is printeda = 3
is an assignment, I think nothing is printed because it does not work witheval
None
results toNone
, so nothing is printedprint(None)
results toNone
(because theprint
function returns nothing), so nothing is printed. However, theprint
function itself printed theNone
.
I honestly didn't read your code, but here's a function that takes a string with code and produces the same output as the interpreter would:
definteractive(code):
try:
result = eval(code)
if result isnotNone:
print(repr(result))
except SyntaxError:
exec(code)
Post a Comment for "Interpreter-style Output In Python 3 (maybe About Sys.displayhook?)"