How can I emulate the behavior of the python2 print statement in python3?












0















Sometimes I am putting together some quick code and have long or parentheses-enclosed expressions. I want to be able to print them quickly and easily with no other fancy stuff or **kwargs. I would like to be able to use the print keyword as in python2 for such usage. How can I do that?










share|improve this question



























    0















    Sometimes I am putting together some quick code and have long or parentheses-enclosed expressions. I want to be able to print them quickly and easily with no other fancy stuff or **kwargs. I would like to be able to use the print keyword as in python2 for such usage. How can I do that?










    share|improve this question

























      0












      0








      0








      Sometimes I am putting together some quick code and have long or parentheses-enclosed expressions. I want to be able to print them quickly and easily with no other fancy stuff or **kwargs. I would like to be able to use the print keyword as in python2 for such usage. How can I do that?










      share|improve this question














      Sometimes I am putting together some quick code and have long or parentheses-enclosed expressions. I want to be able to print them quickly and easily with no other fancy stuff or **kwargs. I would like to be able to use the print keyword as in python2 for such usage. How can I do that?







      python-3.x python-2.7 printing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 6:16









      AalokAalok

      5021518




      5021518
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Here's a hack that uses operator overloading with which you could at least emulate the print keyword to some extent.



          If you were to write a class and overload any of its operator(a,b) methods, you would be able to write anything you wanted in the method body and pass some value as an RHS to the operator, mathematically speaking. For example, here's a class:



          class printkw:
          def __lshift__(self, value=None):
          print(value)
          return value


          Then if you do something like



          printk = printkw()
          printk << "hello world"


          then it would produce the output



          hello world


          If you wanted to treat it totally anonymously, you could get away with



          printkw() << "hello world"


          although it looks much more unaesthetic now.



          The operator you choose to overload can be anything; even the = assignment operator, however, I personally prefer the lshift operator because it makes it look similar to C++. Also, the assignment operator doesn't make semantic sense: you don't assign something you want to output. However, it's really up to your preference.



          Of course, this is what I already called it, a hack, and will probably be frowned upon. Certainly, I do not use such a hack too often (only about twice till now, to be honest), and do not recommend it either. However, sometimes you just have very long statements, or sometimes you want to debug and write something quick and dirty and having to parenthesize the expression properly can be tedious, which is when I use this hack. For regular use, please avoid using it as it may break printing where others expect it to be the standard print() function but you have instead used the above-mentioned keyword emulation.






          share|improve this answer























            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%2f53332424%2fhow-can-i-emulate-the-behavior-of-the-python2-print-statement-in-python3%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Here's a hack that uses operator overloading with which you could at least emulate the print keyword to some extent.



            If you were to write a class and overload any of its operator(a,b) methods, you would be able to write anything you wanted in the method body and pass some value as an RHS to the operator, mathematically speaking. For example, here's a class:



            class printkw:
            def __lshift__(self, value=None):
            print(value)
            return value


            Then if you do something like



            printk = printkw()
            printk << "hello world"


            then it would produce the output



            hello world


            If you wanted to treat it totally anonymously, you could get away with



            printkw() << "hello world"


            although it looks much more unaesthetic now.



            The operator you choose to overload can be anything; even the = assignment operator, however, I personally prefer the lshift operator because it makes it look similar to C++. Also, the assignment operator doesn't make semantic sense: you don't assign something you want to output. However, it's really up to your preference.



            Of course, this is what I already called it, a hack, and will probably be frowned upon. Certainly, I do not use such a hack too often (only about twice till now, to be honest), and do not recommend it either. However, sometimes you just have very long statements, or sometimes you want to debug and write something quick and dirty and having to parenthesize the expression properly can be tedious, which is when I use this hack. For regular use, please avoid using it as it may break printing where others expect it to be the standard print() function but you have instead used the above-mentioned keyword emulation.






            share|improve this answer




























              0














              Here's a hack that uses operator overloading with which you could at least emulate the print keyword to some extent.



              If you were to write a class and overload any of its operator(a,b) methods, you would be able to write anything you wanted in the method body and pass some value as an RHS to the operator, mathematically speaking. For example, here's a class:



              class printkw:
              def __lshift__(self, value=None):
              print(value)
              return value


              Then if you do something like



              printk = printkw()
              printk << "hello world"


              then it would produce the output



              hello world


              If you wanted to treat it totally anonymously, you could get away with



              printkw() << "hello world"


              although it looks much more unaesthetic now.



              The operator you choose to overload can be anything; even the = assignment operator, however, I personally prefer the lshift operator because it makes it look similar to C++. Also, the assignment operator doesn't make semantic sense: you don't assign something you want to output. However, it's really up to your preference.



              Of course, this is what I already called it, a hack, and will probably be frowned upon. Certainly, I do not use such a hack too often (only about twice till now, to be honest), and do not recommend it either. However, sometimes you just have very long statements, or sometimes you want to debug and write something quick and dirty and having to parenthesize the expression properly can be tedious, which is when I use this hack. For regular use, please avoid using it as it may break printing where others expect it to be the standard print() function but you have instead used the above-mentioned keyword emulation.






              share|improve this answer


























                0












                0








                0







                Here's a hack that uses operator overloading with which you could at least emulate the print keyword to some extent.



                If you were to write a class and overload any of its operator(a,b) methods, you would be able to write anything you wanted in the method body and pass some value as an RHS to the operator, mathematically speaking. For example, here's a class:



                class printkw:
                def __lshift__(self, value=None):
                print(value)
                return value


                Then if you do something like



                printk = printkw()
                printk << "hello world"


                then it would produce the output



                hello world


                If you wanted to treat it totally anonymously, you could get away with



                printkw() << "hello world"


                although it looks much more unaesthetic now.



                The operator you choose to overload can be anything; even the = assignment operator, however, I personally prefer the lshift operator because it makes it look similar to C++. Also, the assignment operator doesn't make semantic sense: you don't assign something you want to output. However, it's really up to your preference.



                Of course, this is what I already called it, a hack, and will probably be frowned upon. Certainly, I do not use such a hack too often (only about twice till now, to be honest), and do not recommend it either. However, sometimes you just have very long statements, or sometimes you want to debug and write something quick and dirty and having to parenthesize the expression properly can be tedious, which is when I use this hack. For regular use, please avoid using it as it may break printing where others expect it to be the standard print() function but you have instead used the above-mentioned keyword emulation.






                share|improve this answer













                Here's a hack that uses operator overloading with which you could at least emulate the print keyword to some extent.



                If you were to write a class and overload any of its operator(a,b) methods, you would be able to write anything you wanted in the method body and pass some value as an RHS to the operator, mathematically speaking. For example, here's a class:



                class printkw:
                def __lshift__(self, value=None):
                print(value)
                return value


                Then if you do something like



                printk = printkw()
                printk << "hello world"


                then it would produce the output



                hello world


                If you wanted to treat it totally anonymously, you could get away with



                printkw() << "hello world"


                although it looks much more unaesthetic now.



                The operator you choose to overload can be anything; even the = assignment operator, however, I personally prefer the lshift operator because it makes it look similar to C++. Also, the assignment operator doesn't make semantic sense: you don't assign something you want to output. However, it's really up to your preference.



                Of course, this is what I already called it, a hack, and will probably be frowned upon. Certainly, I do not use such a hack too often (only about twice till now, to be honest), and do not recommend it either. However, sometimes you just have very long statements, or sometimes you want to debug and write something quick and dirty and having to parenthesize the expression properly can be tedious, which is when I use this hack. For regular use, please avoid using it as it may break printing where others expect it to be the standard print() function but you have instead used the above-mentioned keyword emulation.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 6:16









                AalokAalok

                5021518




                5021518
































                    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%2f53332424%2fhow-can-i-emulate-the-behavior-of-the-python2-print-statement-in-python3%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

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python