[Python-ideas] Pythonic Dates, Times, and Deltas
Alexander Belopolsky
alexander.belopolsky at gmail.com
Thu Oct 14 00:52:52 CEST 2010
On Wed, Oct 13, 2010 at 5:45 PM, Daniel G. Taylor
<dan at programmer-art.org> wrote:
..
>> * Make it easy to make a Date from anything - a timestamp, date,
>> datetime, tuple, etc.
>>
>>>>> from datetime import *
>>>>> datetime.utcfromtimestamp(0)
>>
>> datetime.datetime(1970, 1, 1, 0, 0)
>>>>>
>>>>> datetime.utcfromtimestamp(0).date()
>>
>> datetime.date(1970, 1, 1)
>
> Why does it not have this in the constructor?
Because "explicit is better than implicit."
> Where else in the standard lib does anything behave like this?
float.fromhex is one example.
This said, if I was starting from scratch, I would make date/datetime
constructors take a single positional argument that could be a string
(interpreted as ISO timestamp), tuple (broken down components), or
another date/datetime object. This would make date/datetime
constructors more similar to those of numeric types. I would not add
datetime(int) or datetime(float), however, because numeric timestamps
are too ambiguous and not necessary for purely calendaric
calculations.
More information about the Python-ideas
mailing list