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 prints" like you thought.
3 + 4results to7, sorepr(7)is printeda = 3is an assignment, I think nothing is printed because it does not work withevalNoneresults toNone, so nothing is printedprint(None)results toNone(because theprintfunction returns nothing), so nothing is printed. However, theprintfunction 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?)"