[Python-ideas] strptime without second argument as an inverse to __str__

Alexander Belopolsky alexander.belopolsky at gmail.com
Wed Aug 6 17:42:55 CEST 2014


On Wed, Aug 6, 2014 at 11:19 AM, Wolfgang Maier <
wolfgang.maier at biologie.uni-freiburg.de> wrote:

> What are the downsides of:
>>
>> dt = datetime.datetime.now()    # assuming this works ;)
>> sdt = str(dt)
>> ndt = datetime.datetime(std)
>> print(dt == ndt)
>> #True
>>
>>
> I'll refrain from mentioning "explicit is better than implicit" ;)
>
> It's just that it seems to be a design pattern of the datetime class to
> provide alternative constructors as classmethods instead of doing magic
> things in __new__ .


I don't think this is a "design pattern".  In Python 2, having date(str)
constructor was blocked by some magic that is there to support unpickling:

>>> from datetime import date
>>> date('\x07\xd0\x01\x01')
datetime.date(2000, 1, 1)

This is no longer an issue in Python 3.

Note that if we allow date('2000-01-01'), this may become a more readable
and efficient alternative to date(2001, 1, 1).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140806/50e3ccf0/attachment-0001.html>


More information about the Python-ideas mailing list