Why does typing a variable (or expression) print the value to stdout?
Clash Royale CLAN TAG#URR8PPP
Take this example:
>>> 5+10
15
>>> a = 5 + 10
>>> a
15
How and why does Python do this without an explicit print statement?
If I do the same thing in an IPython cell, only the last such value is actually printed on stdout in this way:
In[1]: 5+10
1
Out[1]: 1
Why does this happen?
python ipython read-eval-print-loop python-interactive
add a comment |
Take this example:
>>> 5+10
15
>>> a = 5 + 10
>>> a
15
How and why does Python do this without an explicit print statement?
If I do the same thing in an IPython cell, only the last such value is actually printed on stdout in this way:
In[1]: 5+10
1
Out[1]: 1
Why does this happen?
python ipython read-eval-print-loop python-interactive
add a comment |
Take this example:
>>> 5+10
15
>>> a = 5 + 10
>>> a
15
How and why does Python do this without an explicit print statement?
If I do the same thing in an IPython cell, only the last such value is actually printed on stdout in this way:
In[1]: 5+10
1
Out[1]: 1
Why does this happen?
python ipython read-eval-print-loop python-interactive
Take this example:
>>> 5+10
15
>>> a = 5 + 10
>>> a
15
How and why does Python do this without an explicit print statement?
If I do the same thing in an IPython cell, only the last such value is actually printed on stdout in this way:
In[1]: 5+10
1
Out[1]: 1
Why does this happen?
python ipython read-eval-print-loop python-interactive
python ipython read-eval-print-loop python-interactive
edited Feb 25 at 12:41
Peter Mortensen
13.8k1987113
13.8k1987113
asked Feb 25 at 4:19
Chayan GhoshChayan Ghosh
1507
1507
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
>>> 123
can haz 123?
>>> False
can haz False?
>>> None
can haz None?
And also set it back to normal:
>>> sys.displayhook = sys.__displayhook__
>>> 3
3
In the default Python repl, sys.displayhook
is
>>> import sys;
>>> sys.displayhook
<built-in function displayhook>
but in IPython it's
In [1]: import sys
In [2]: sys.displayhook
Out[2]: <IPython.terminal.prompts.RichPromptDisplayHook at 0x7f630717fa58>
So that's why you see different behavior between Python and IPython.
add a comment |
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.
1
any comment on the IPython behavior?
– Chayan Ghosh
Feb 25 at 4:25
2
Let's say CPython in interactive mode works like that.
– Klaus D.
Feb 25 at 4:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54859437%2fwhy-does-typing-a-variable-or-expression-print-the-value-to-stdout%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
>>> 123
can haz 123?
>>> False
can haz False?
>>> None
can haz None?
And also set it back to normal:
>>> sys.displayhook = sys.__displayhook__
>>> 3
3
In the default Python repl, sys.displayhook
is
>>> import sys;
>>> sys.displayhook
<built-in function displayhook>
but in IPython it's
In [1]: import sys
In [2]: sys.displayhook
Out[2]: <IPython.terminal.prompts.RichPromptDisplayHook at 0x7f630717fa58>
So that's why you see different behavior between Python and IPython.
add a comment |
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
>>> 123
can haz 123?
>>> False
can haz False?
>>> None
can haz None?
And also set it back to normal:
>>> sys.displayhook = sys.__displayhook__
>>> 3
3
In the default Python repl, sys.displayhook
is
>>> import sys;
>>> sys.displayhook
<built-in function displayhook>
but in IPython it's
In [1]: import sys
In [2]: sys.displayhook
Out[2]: <IPython.terminal.prompts.RichPromptDisplayHook at 0x7f630717fa58>
So that's why you see different behavior between Python and IPython.
add a comment |
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
>>> 123
can haz 123?
>>> False
can haz False?
>>> None
can haz None?
And also set it back to normal:
>>> sys.displayhook = sys.__displayhook__
>>> 3
3
In the default Python repl, sys.displayhook
is
>>> import sys;
>>> sys.displayhook
<built-in function displayhook>
but in IPython it's
In [1]: import sys
In [2]: sys.displayhook
Out[2]: <IPython.terminal.prompts.RichPromptDisplayHook at 0x7f630717fa58>
So that's why you see different behavior between Python and IPython.
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
>>> 123
can haz 123?
>>> False
can haz False?
>>> None
can haz None?
And also set it back to normal:
>>> sys.displayhook = sys.__displayhook__
>>> 3
3
In the default Python repl, sys.displayhook
is
>>> import sys;
>>> sys.displayhook
<built-in function displayhook>
but in IPython it's
In [1]: import sys
In [2]: sys.displayhook
Out[2]: <IPython.terminal.prompts.RichPromptDisplayHook at 0x7f630717fa58>
So that's why you see different behavior between Python and IPython.
edited Feb 25 at 13:52
answered Feb 25 at 4:33
kojirokojiro
54.1k1390142
54.1k1390142
add a comment |
add a comment |
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.
1
any comment on the IPython behavior?
– Chayan Ghosh
Feb 25 at 4:25
2
Let's say CPython in interactive mode works like that.
– Klaus D.
Feb 25 at 4:25
add a comment |
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.
1
any comment on the IPython behavior?
– Chayan Ghosh
Feb 25 at 4:25
2
Let's say CPython in interactive mode works like that.
– Klaus D.
Feb 25 at 4:25
add a comment |
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.
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.
edited Feb 25 at 12:43
Peter Mortensen
13.8k1987113
13.8k1987113
answered Feb 25 at 4:21
U9-ForwardU9-Forward
16.9k51643
16.9k51643
1
any comment on the IPython behavior?
– Chayan Ghosh
Feb 25 at 4:25
2
Let's say CPython in interactive mode works like that.
– Klaus D.
Feb 25 at 4:25
add a comment |
1
any comment on the IPython behavior?
– Chayan Ghosh
Feb 25 at 4:25
2
Let's say CPython in interactive mode works like that.
– Klaus D.
Feb 25 at 4:25
1
1
any comment on the IPython behavior?
– Chayan Ghosh
Feb 25 at 4:25
any comment on the IPython behavior?
– Chayan Ghosh
Feb 25 at 4:25
2
2
Let's say CPython in interactive mode works like that.
– Klaus D.
Feb 25 at 4:25
Let's say CPython in interactive mode works like that.
– Klaus D.
Feb 25 at 4:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54859437%2fwhy-does-typing-a-variable-or-expression-print-the-value-to-stdout%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown