1

I am going to be holding a Python workshop to teach some beginner level programmers at my college Python.

I am looking for a way to modify the default behavior of Python interactive sessions so that expressions do not automatically print out results. For example, take this following interactive session:

wil@calcifer:~$ python
Python 2.7.3 (default, Aug  1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 5
>>> y = 7
>>> x + y
12
>>> first_name = "Test"
>>> last_name = "Person"
>>> first_name + last_name
'TestPerson'
>>>

This would enforce the use of the print statement to avoid confusion later on. This isn't crucial, but I was surprised that I was unable to find any settings for this anywhere.

I would be able to preconfigure the machine beforehand, so any solutions would work. Does anyone know if this is possible, or what change to the interactive sessions creates this effect?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
WilHall
  • 11,644
  • 6
  • 31
  • 53
  • 4
    No, please do not do that. The interactive shell is a major debugging tool that I'd expect Python programmers know how to use. Do not teach with a neutered REPL! – Martijn Pieters Mar 20 '13 at 21:28
  • 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?" – askewchan Mar 20 '13 at 21:32
  • Actually, part of the reason I asked the question was to see what others thought of the idea. You are right, though - there would be something missing without it. If either of you create an answer, I'll mark it as accepted. – WilHall Mar 20 '13 at 21:49
  • 1
    @MartijnPieters: I never start showing Python with this behavior - But I have had problem in the past to get the students understanding the values are not printed when running a program. I can see doing this (my answer bellow shows how) in an intermediate step to some groups. – jsbueno Mar 20 '13 at 22:02

3 Answers3

10

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
>>> print 2 + 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

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • +1, answers the question nicely, but I think it would confuse python learners. – askewchan Mar 20 '13 at 21:56
  • 1
    I see the OP's point, and I am considering opting for this in some class sessions myself. It is sometimes harder to make them understand why the result of each line in a program is not itself printed. – jsbueno Mar 20 '13 at 21:59
  • 1
    Yeah it does go both ways. I suppose confused learners are unavoidable whichever method you choose. – askewchan Mar 20 '13 at 22:01
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 prints.

askewchan
  • 45,161
  • 17
  • 118
  • 134
1

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=5
y=7
z = x+y
ryrich
  • 2,164
  • 16
  • 24
  • Yes, do this since an empty expression doesn't mean much in a python script anyway. What would `x+y` on its own mean otherwise? – askewchan Mar 20 '13 at 21:30