In datetime, why is `year` a property, but `weekday()` a function?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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
add a comment |
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
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
add a comment |
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
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
python python-3.x python-datetime
edited Nov 14 '18 at 11:25
smci
15.7k679110
15.7k679110
asked Nov 14 '18 at 10:52
steenberghsteenbergh
80611433
80611433
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
add a comment |
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
add a comment |
5 Answers
5
active
oldest
votes
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
add a comment |
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.
add a comment |
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
add a comment |
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.
add a comment |
Because weekday need some operations.
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%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
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
add a comment |
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
add a comment |
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
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
answered Nov 14 '18 at 10:58
ksbgksbg
2,24711224
2,24711224
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 14 '18 at 10:56
jppjpp
103k2167117
103k2167117
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 14 '18 at 11:10
nigel222nigel222
2,2121512
2,2121512
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 14 '18 at 11:15
blue_noteblue_note
12.9k32536
12.9k32536
add a comment |
add a comment |
Because weekday need some operations.
add a comment |
Because weekday need some operations.
add a comment |
Because weekday need some operations.
Because weekday need some operations.
answered Nov 14 '18 at 10:57
Michael RatefinjanaharyMichael Ratefinjanahary
212
212
add a comment |
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%2f53298489%2fin-datetime-why-is-year-a-property-but-weekday-a-function%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
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