Disable Automatic Printing In Python Interactive Session
Solution 1:
The expression printing in interactive sessions is mediated via a call to
sys.displayhook
. Just override it to a NOP:
>>>import sys>>>sys.displayhook = lambda x: None>>>2 + 3>>>print2 + 3
5
>>>
In a plain Python session it is wired to sys.stdout. Applications that offer a Python shell on a GUI are expcted to use it to print the expression values back to the user.
You can write your pythonrc.py ( ~/.pythonrc.py file) to do this by default, each time the interpreter is called. Further documentation on sys.displayhook can be found here: http://docs.python.org/3/library/sys.html#sys.displayhook
Solution 2:
I know you didn't come here for teaching advice, but without an output, what's x + y
supposed to do? Why type it at all if you don't want the value to be output or stored anywhere?
Your students will be confused: "what does x + y
do?"
If you want to show some expression, just consistently put a print
statement in front of it, or assign it to a value.
I've found that people are often confused the other way around, so much that they don't know that return
actually gives them the value back, and they always want to use print
. Don't forget that you can define functions interactively, and put the return there. Then you can show them that the function returns that value, even with no print
s.
Solution 3:
I don't think you can configure the shell to suppress the results of expressions, but would it work if you just assigned every expression to a variable? It won't print anything then...
E.G.
x=5y=7z = x+y
Post a Comment for "Disable Automatic Printing In Python Interactive Session"