call function without parenthesis in python





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















what modification i would need to do in the below function computeDifference to get result printed in the console, instead of object message.



i know i need to add parenthesis () to call function to get the result printed in the console, but is there any other way to print the result?



class Difference1:
def __init__(self, a):
self.__elements = a

def computeDifference(self):
self.difference = max(self.__elements)- min(self.__elements)
return self.difference

a = [5,8,9,22,2]
c = Difference1(a)
print(c.computeDifference)









share|improve this question































    3















    what modification i would need to do in the below function computeDifference to get result printed in the console, instead of object message.



    i know i need to add parenthesis () to call function to get the result printed in the console, but is there any other way to print the result?



    class Difference1:
    def __init__(self, a):
    self.__elements = a

    def computeDifference(self):
    self.difference = max(self.__elements)- min(self.__elements)
    return self.difference

    a = [5,8,9,22,2]
    c = Difference1(a)
    print(c.computeDifference)









    share|improve this question



























      3












      3








      3


      1






      what modification i would need to do in the below function computeDifference to get result printed in the console, instead of object message.



      i know i need to add parenthesis () to call function to get the result printed in the console, but is there any other way to print the result?



      class Difference1:
      def __init__(self, a):
      self.__elements = a

      def computeDifference(self):
      self.difference = max(self.__elements)- min(self.__elements)
      return self.difference

      a = [5,8,9,22,2]
      c = Difference1(a)
      print(c.computeDifference)









      share|improve this question
















      what modification i would need to do in the below function computeDifference to get result printed in the console, instead of object message.



      i know i need to add parenthesis () to call function to get the result printed in the console, but is there any other way to print the result?



      class Difference1:
      def __init__(self, a):
      self.__elements = a

      def computeDifference(self):
      self.difference = max(self.__elements)- min(self.__elements)
      return self.difference

      a = [5,8,9,22,2]
      c = Difference1(a)
      print(c.computeDifference)






      python oop






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 23 at 8:53







      Pr Mod

















      asked Nov 8 '18 at 10:12









      Pr ModPr Mod

      1311212




      1311212
























          2 Answers
          2






          active

          oldest

          votes


















          5














          Make it a property



          class Difference1:
          @property
          def computeDifference(self):
          ...

          print(c.computeDifference)


          However, I would change the name to difference. The idea of a property is that you shouldn't know or care whether the value is computed at that time or is stored as an attribute of the object. See uniform access principle.






          share|improve this answer































            2














            You could add a magic function:



            class Difference1:
            ...
            def __str__(self):
            return self.computeDifference()
            ...

            >>> a = [5,8,9,22,2]
            >>> c = Difference1(a)
            >>> print(c)
            20





            share|improve this answer



















            • 1





              thanks for this suggestion.

              – Pr Mod
              Nov 8 '18 at 11:19












            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%2f53205552%2fcall-function-without-parenthesis-in-python%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









            5














            Make it a property



            class Difference1:
            @property
            def computeDifference(self):
            ...

            print(c.computeDifference)


            However, I would change the name to difference. The idea of a property is that you shouldn't know or care whether the value is computed at that time or is stored as an attribute of the object. See uniform access principle.






            share|improve this answer




























              5














              Make it a property



              class Difference1:
              @property
              def computeDifference(self):
              ...

              print(c.computeDifference)


              However, I would change the name to difference. The idea of a property is that you shouldn't know or care whether the value is computed at that time or is stored as an attribute of the object. See uniform access principle.






              share|improve this answer


























                5












                5








                5







                Make it a property



                class Difference1:
                @property
                def computeDifference(self):
                ...

                print(c.computeDifference)


                However, I would change the name to difference. The idea of a property is that you shouldn't know or care whether the value is computed at that time or is stored as an attribute of the object. See uniform access principle.






                share|improve this answer













                Make it a property



                class Difference1:
                @property
                def computeDifference(self):
                ...

                print(c.computeDifference)


                However, I would change the name to difference. The idea of a property is that you shouldn't know or care whether the value is computed at that time or is stored as an attribute of the object. See uniform access principle.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 '18 at 10:13









                blue_noteblue_note

                12.8k32536




                12.8k32536

























                    2














                    You could add a magic function:



                    class Difference1:
                    ...
                    def __str__(self):
                    return self.computeDifference()
                    ...

                    >>> a = [5,8,9,22,2]
                    >>> c = Difference1(a)
                    >>> print(c)
                    20





                    share|improve this answer



















                    • 1





                      thanks for this suggestion.

                      – Pr Mod
                      Nov 8 '18 at 11:19
















                    2














                    You could add a magic function:



                    class Difference1:
                    ...
                    def __str__(self):
                    return self.computeDifference()
                    ...

                    >>> a = [5,8,9,22,2]
                    >>> c = Difference1(a)
                    >>> print(c)
                    20





                    share|improve this answer



















                    • 1





                      thanks for this suggestion.

                      – Pr Mod
                      Nov 8 '18 at 11:19














                    2












                    2








                    2







                    You could add a magic function:



                    class Difference1:
                    ...
                    def __str__(self):
                    return self.computeDifference()
                    ...

                    >>> a = [5,8,9,22,2]
                    >>> c = Difference1(a)
                    >>> print(c)
                    20





                    share|improve this answer













                    You could add a magic function:



                    class Difference1:
                    ...
                    def __str__(self):
                    return self.computeDifference()
                    ...

                    >>> a = [5,8,9,22,2]
                    >>> c = Difference1(a)
                    >>> print(c)
                    20






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 8 '18 at 10:19









                    Peter WoodPeter Wood

                    16.7k33876




                    16.7k33876








                    • 1





                      thanks for this suggestion.

                      – Pr Mod
                      Nov 8 '18 at 11:19














                    • 1





                      thanks for this suggestion.

                      – Pr Mod
                      Nov 8 '18 at 11:19








                    1




                    1





                    thanks for this suggestion.

                    – Pr Mod
                    Nov 8 '18 at 11:19





                    thanks for this suggestion.

                    – Pr Mod
                    Nov 8 '18 at 11:19


















                    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%2f53205552%2fcall-function-without-parenthesis-in-python%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







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()