Why does typing a variable (or expression) print the value to stdout?Calling a function of a module by using...

How long has this character been impersonating a Starfleet Officer?

Can me and my friend spend the summer in Canada (6 weeks) at 16 years old without an adult?

How to change a .eps figure to standalone class?

What to do with threats of blacklisting?

How to stop the animation and freeze the image when pressing the `Stop` button

Coombinatorics- The number of ways of choosing with parameters

Does diversity provide anything that meritocracy does not?

How do I add a strong "onion flavor" to the biryani (in restaurant style)?

How do I avoid the "chosen hero" feeling?

Why didn't Tom Riddle take the presence of Fawkes and the Sorting Hat as more of a threat?

Is there a file that always exists and a 'normal' user can't lstat it?

How to extract specific values/fields from the text file?

Is the fingering of thirds flexible or do I have to follow the rules?

Equivalent of "illegal" for violating civil law

What is the draw frequency for 3 consecutive games (same players; amateur level)?

Eww, those bytes are gross

Is it possible to rotate the Isolines on a Surface Using `MeshFunction`?

How is this property called for mod?

Possible issue with my W4 and tax return

"Starve to death" Vs. "Starve to the point of death"

Illustrator to chemdraw

Potential client have a problematic employee I can't work with

Is it possible to detect 100% of SQLi with a simple regex?

Co-worker sabotaging/undoing my work (software development)



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


Calling a function of a module by using its name (a string)How to return multiple values from a function?Convert bytes to a string?Emulate a do-while loop in Python?“Least Astonishment” and the Mutable Default ArgumentProper way to declare custom exceptions in modern Python?Python string formatting: % vs. .formatFastest way to check if a value exist in a listUsing IPython notebooks under version controlWhy is “1000000000000000 in range(1000000000000001)” so fast in Python 3?













8















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?










share|improve this question





























    8















    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?










    share|improve this question



























      8












      8








      8








      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?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 45 mins ago









      Peter Mortensen

      13.7k1986112




      13.7k1986112










      asked 9 hours ago









      Chayan GhoshChayan Ghosh

      1056




      1056
























          2 Answers
          2






          active

          oldest

          votes


















          20














          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.






          share|improve this answer

































            1














            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.






            share|improve this answer


























            • any comment on the IPython behavior?

              – Chayan Ghosh
              9 hours ago






            • 1





              Let's say CPython in interactive mode works like that.

              – Klaus D.
              9 hours ago











            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            20














            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.






            share|improve this answer






























              20














              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.






              share|improve this answer




























                20












                20








                20







                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.






                share|improve this answer















                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 45 mins ago









                Peter Mortensen

                13.7k1986112




                13.7k1986112










                answered 8 hours ago









                kojirokojiro

                53.6k1387139




                53.6k1387139

























                    1














                    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.






                    share|improve this answer


























                    • any comment on the IPython behavior?

                      – Chayan Ghosh
                      9 hours ago






                    • 1





                      Let's say CPython in interactive mode works like that.

                      – Klaus D.
                      9 hours ago
















                    1














                    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.






                    share|improve this answer


























                    • any comment on the IPython behavior?

                      – Chayan Ghosh
                      9 hours ago






                    • 1





                      Let's say CPython in interactive mode works like that.

                      – Klaus D.
                      9 hours ago














                    1












                    1








                    1







                    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.






                    share|improve this answer















                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 43 mins ago









                    Peter Mortensen

                    13.7k1986112




                    13.7k1986112










                    answered 9 hours ago









                    U9-ForwardU9-Forward

                    15.7k51540




                    15.7k51540













                    • any comment on the IPython behavior?

                      – Chayan Ghosh
                      9 hours ago






                    • 1





                      Let's say CPython in interactive mode works like that.

                      – Klaus D.
                      9 hours ago



















                    • any comment on the IPython behavior?

                      – Chayan Ghosh
                      9 hours ago






                    • 1





                      Let's say CPython in interactive mode works like that.

                      – Klaus D.
                      9 hours ago

















                    any comment on the IPython behavior?

                    – Chayan Ghosh
                    9 hours ago





                    any comment on the IPython behavior?

                    – Chayan Ghosh
                    9 hours ago




                    1




                    1





                    Let's say CPython in interactive mode works like that.

                    – Klaus D.
                    9 hours ago





                    Let's say CPython in interactive mode works like that.

                    – Klaus D.
                    9 hours ago


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Щит и меч (фильм) Содержание Названия серий | Сюжет |...

                    Венесуэла на летних Олимпийских играх 2000 Содержание Состав...

                    Meter-Bus Содержание Параметры шины | Стандартизация |...