Why does typing a variable (or expression) print the value to stdout? Why does typing a variable (or expression) print the value to stdout? python python

Why does typing a variable (or expression) print the value to stdout?


When Python is in "interactive" mode, it enables certain behaviors it doesn't have in non-interactive mode. For example, sys.displayhook, originally specified in PEP 217.

If value is not None, this function prints it to sys.stdout, and saves it in __builtin__._.

sys.displayhook is called on the result of evaluating an expression entered in an interactive Python session.

You can modify this behavior:

>>> import sys>>> def shook(expr):...   print(f'can haz {expr}?')...>>> sys.displayhook = shook>>> 123can haz 123?>>> Falsecan haz False?>>> Nonecan haz None?

And also set it back to normal:

>>> sys.displayhook = sys.__displayhook__>>> 33

In the default Python repl, sys.displayhook is

>>> import sys;>>> sys.displayhook<built-in function displayhook>

but in IPython it's

In [1]: import sysIn [2]: sys.displayhookOut[2]: <IPython.terminal.prompts.RichPromptDisplayHook at 0x7f630717fa58>

So that's why you see different behavior between Python and IPython.


That's how all interpreters work. They don't need any print, but one thing, and without print they do the repr of everything, and print doesn't, example:

>>> 'blah''blah'>>> print('blah')blah>>> 

Look at the quotes.

Also see this:

>>> print(repr('blah'))'blah'>>> 

repr does the same.