In datetime, why is `year` a property, but `weekday()` a function?










3















I have a question about this piece of Python 3 script:



import datetime
d = datetime.date.today()

print(d.year)
print(d.weekday())


TIO



Why is d.year without parentheses, but d.weekday() isn't? Why is one a property and the other a function?










share|improve this question



















  • 2





    year, month and day are base constituents of the class datetime so properties. Getting the weekday is the result of a computation involving year, month and day so it is a method.

    – PilouPili
    Nov 14 '18 at 10:56
















3















I have a question about this piece of Python 3 script:



import datetime
d = datetime.date.today()

print(d.year)
print(d.weekday())


TIO



Why is d.year without parentheses, but d.weekday() isn't? Why is one a property and the other a function?










share|improve this question



















  • 2





    year, month and day are base constituents of the class datetime so properties. Getting the weekday is the result of a computation involving year, month and day so it is a method.

    – PilouPili
    Nov 14 '18 at 10:56














3












3








3








I have a question about this piece of Python 3 script:



import datetime
d = datetime.date.today()

print(d.year)
print(d.weekday())


TIO



Why is d.year without parentheses, but d.weekday() isn't? Why is one a property and the other a function?










share|improve this question
















I have a question about this piece of Python 3 script:



import datetime
d = datetime.date.today()

print(d.year)
print(d.weekday())


TIO



Why is d.year without parentheses, but d.weekday() isn't? Why is one a property and the other a function?







python python-3.x python-datetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 11:25









smci

15.2k676108




15.2k676108










asked Nov 14 '18 at 10:52









steenberghsteenbergh

80111433




80111433







  • 2





    year, month and day are base constituents of the class datetime so properties. Getting the weekday is the result of a computation involving year, month and day so it is a method.

    – PilouPili
    Nov 14 '18 at 10:56













  • 2





    year, month and day are base constituents of the class datetime so properties. Getting the weekday is the result of a computation involving year, month and day so it is a method.

    – PilouPili
    Nov 14 '18 at 10:56








2




2





year, month and day are base constituents of the class datetime so properties. Getting the weekday is the result of a computation involving year, month and day so it is a method.

– PilouPili
Nov 14 '18 at 10:56






year, month and day are base constituents of the class datetime so properties. Getting the weekday is the result of a computation involving year, month and day so it is a method.

– PilouPili
Nov 14 '18 at 10:56













5 Answers
5






active

oldest

votes


















2














Probably because year is a required argument when constructing a datetime.date object. No computation is required within the object to access the year value. See the implementation:



class date:
def __new__(cls, year, month=None, day=None):
...


Whereas weekday needs to be computed:



def weekday(self):
"Return day of the week, where Monday == 0 ... Sunday == 6."
return (self.toordinal() + 6) % 7





share|improve this answer






























    2














    Because year is required for defining datetime.date objects and is subsequently set as a property of the class:




    class datetime.date(year, month, day)


    All arguments are required.




    While weekday needs to be calculated via a method from year, month and day.






    share|improve this answer






























      1














      There's no particular reason I can think of. It's a module with a documented API which says weekday() is a method not a property. One can argue whether it should have been otherwise, but as soon as the module was released and people wrote code that depended on this interface, it could no longer be changed. (Other than by deprecating the module and releasing datetime2, which the Python gods have indeed done for a few modules that were much more badly broken by design).



      If it really bugs you you can 'fix' it. In Python3 (simplified super):



      class MyDate( datetime.date):
      @property
      def weekday(self):
      return super().weekday()

      >>> d = MyDate.today()
      >>> d
      MyDate(2018, 11, 14)
      >>> d.weekday
      2





      share|improve this answer






























        1














        For no good reason. It's inconsistent, and violates some basic principles. Don't read too much into it, it was probably some instant decision that stuck.






        share|improve this answer






























          0














          Because weekday need some operations.






          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%2f53298489%2fin-datetime-why-is-year-a-property-but-weekday-a-function%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            Probably because year is a required argument when constructing a datetime.date object. No computation is required within the object to access the year value. See the implementation:



            class date:
            def __new__(cls, year, month=None, day=None):
            ...


            Whereas weekday needs to be computed:



            def weekday(self):
            "Return day of the week, where Monday == 0 ... Sunday == 6."
            return (self.toordinal() + 6) % 7





            share|improve this answer



























              2














              Probably because year is a required argument when constructing a datetime.date object. No computation is required within the object to access the year value. See the implementation:



              class date:
              def __new__(cls, year, month=None, day=None):
              ...


              Whereas weekday needs to be computed:



              def weekday(self):
              "Return day of the week, where Monday == 0 ... Sunday == 6."
              return (self.toordinal() + 6) % 7





              share|improve this answer

























                2












                2








                2







                Probably because year is a required argument when constructing a datetime.date object. No computation is required within the object to access the year value. See the implementation:



                class date:
                def __new__(cls, year, month=None, day=None):
                ...


                Whereas weekday needs to be computed:



                def weekday(self):
                "Return day of the week, where Monday == 0 ... Sunday == 6."
                return (self.toordinal() + 6) % 7





                share|improve this answer













                Probably because year is a required argument when constructing a datetime.date object. No computation is required within the object to access the year value. See the implementation:



                class date:
                def __new__(cls, year, month=None, day=None):
                ...


                Whereas weekday needs to be computed:



                def weekday(self):
                "Return day of the week, where Monday == 0 ... Sunday == 6."
                return (self.toordinal() + 6) % 7






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 10:58









                ksbgksbg

                2,2371224




                2,2371224























                    2














                    Because year is required for defining datetime.date objects and is subsequently set as a property of the class:




                    class datetime.date(year, month, day)


                    All arguments are required.




                    While weekday needs to be calculated via a method from year, month and day.






                    share|improve this answer



























                      2














                      Because year is required for defining datetime.date objects and is subsequently set as a property of the class:




                      class datetime.date(year, month, day)


                      All arguments are required.




                      While weekday needs to be calculated via a method from year, month and day.






                      share|improve this answer

























                        2












                        2








                        2







                        Because year is required for defining datetime.date objects and is subsequently set as a property of the class:




                        class datetime.date(year, month, day)


                        All arguments are required.




                        While weekday needs to be calculated via a method from year, month and day.






                        share|improve this answer













                        Because year is required for defining datetime.date objects and is subsequently set as a property of the class:




                        class datetime.date(year, month, day)


                        All arguments are required.




                        While weekday needs to be calculated via a method from year, month and day.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 14 '18 at 10:56









                        jppjpp

                        101k2164114




                        101k2164114





















                            1














                            There's no particular reason I can think of. It's a module with a documented API which says weekday() is a method not a property. One can argue whether it should have been otherwise, but as soon as the module was released and people wrote code that depended on this interface, it could no longer be changed. (Other than by deprecating the module and releasing datetime2, which the Python gods have indeed done for a few modules that were much more badly broken by design).



                            If it really bugs you you can 'fix' it. In Python3 (simplified super):



                            class MyDate( datetime.date):
                            @property
                            def weekday(self):
                            return super().weekday()

                            >>> d = MyDate.today()
                            >>> d
                            MyDate(2018, 11, 14)
                            >>> d.weekday
                            2





                            share|improve this answer



























                              1














                              There's no particular reason I can think of. It's a module with a documented API which says weekday() is a method not a property. One can argue whether it should have been otherwise, but as soon as the module was released and people wrote code that depended on this interface, it could no longer be changed. (Other than by deprecating the module and releasing datetime2, which the Python gods have indeed done for a few modules that were much more badly broken by design).



                              If it really bugs you you can 'fix' it. In Python3 (simplified super):



                              class MyDate( datetime.date):
                              @property
                              def weekday(self):
                              return super().weekday()

                              >>> d = MyDate.today()
                              >>> d
                              MyDate(2018, 11, 14)
                              >>> d.weekday
                              2





                              share|improve this answer

























                                1












                                1








                                1







                                There's no particular reason I can think of. It's a module with a documented API which says weekday() is a method not a property. One can argue whether it should have been otherwise, but as soon as the module was released and people wrote code that depended on this interface, it could no longer be changed. (Other than by deprecating the module and releasing datetime2, which the Python gods have indeed done for a few modules that were much more badly broken by design).



                                If it really bugs you you can 'fix' it. In Python3 (simplified super):



                                class MyDate( datetime.date):
                                @property
                                def weekday(self):
                                return super().weekday()

                                >>> d = MyDate.today()
                                >>> d
                                MyDate(2018, 11, 14)
                                >>> d.weekday
                                2





                                share|improve this answer













                                There's no particular reason I can think of. It's a module with a documented API which says weekday() is a method not a property. One can argue whether it should have been otherwise, but as soon as the module was released and people wrote code that depended on this interface, it could no longer be changed. (Other than by deprecating the module and releasing datetime2, which the Python gods have indeed done for a few modules that were much more badly broken by design).



                                If it really bugs you you can 'fix' it. In Python3 (simplified super):



                                class MyDate( datetime.date):
                                @property
                                def weekday(self):
                                return super().weekday()

                                >>> d = MyDate.today()
                                >>> d
                                MyDate(2018, 11, 14)
                                >>> d.weekday
                                2






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 14 '18 at 11:10









                                nigel222nigel222

                                2,1151512




                                2,1151512





















                                    1














                                    For no good reason. It's inconsistent, and violates some basic principles. Don't read too much into it, it was probably some instant decision that stuck.






                                    share|improve this answer



























                                      1














                                      For no good reason. It's inconsistent, and violates some basic principles. Don't read too much into it, it was probably some instant decision that stuck.






                                      share|improve this answer

























                                        1












                                        1








                                        1







                                        For no good reason. It's inconsistent, and violates some basic principles. Don't read too much into it, it was probably some instant decision that stuck.






                                        share|improve this answer













                                        For no good reason. It's inconsistent, and violates some basic principles. Don't read too much into it, it was probably some instant decision that stuck.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 14 '18 at 11:15









                                        blue_noteblue_note

                                        11.9k32435




                                        11.9k32435





















                                            0














                                            Because weekday need some operations.






                                            share|improve this answer



























                                              0














                                              Because weekday need some operations.






                                              share|improve this answer

























                                                0












                                                0








                                                0







                                                Because weekday need some operations.






                                                share|improve this answer













                                                Because weekday need some operations.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 14 '18 at 10:57









                                                Michael RatefinjanaharyMichael Ratefinjanahary

                                                212




                                                212



























                                                    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%2f53298489%2fin-datetime-why-is-year-a-property-but-weekday-a-function%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

                                                    Use pre created SQLite database for Android project in kotlin

                                                    Darth Vader #20

                                                    Ondo