[Python-ideas] Making datetime __str__ and isoformat more consistent

Eric V. Smith eric at trueblade.com
Fri Nov 1 23:20:48 CET 2013


On 11/1/2013 6:14 PM, Mark Lawrence wrote:
> On 01/11/2013 21:29, Ethan Furman wrote:
>> On 11/01/2013 01:37 PM, MRAB wrote:
>>> On 01/11/2013 19:49, Ethan Furman wrote:
>>>> On 11/01/2013 11:15 AM, Skip Montanaro wrote:
>>>>>
>>>>> http://bugs.python.org/issue19475
>>>>>
>>>>> Do others agree with me that consistency in this situation is better
>>>>> than
>>>>> the current behavior?
>>>>
>>>> I haven't seen the arguments in favor of this awkward behavior, so I
>>>> may change my mind, but at the moment I would
>>>> certainly argue for consistency: either emit microseconds in __str__
>>>> or ignore remaining microseconds or ignore a
>>>> trailing %f (or all three ;) .
>>>>
>>> Suppose there were, say, 900000 microseconds, i.e. 0.9 seconds?
>>>
>>> If the microseconds aren't shown by __str__, should it truncate or
>>> round?
>>
>> My first (and preferred) option was to emit the microseconds (even if 0).
>>
>> At the heart of this issue is: If datetime.datetime is not going to
>> always emit the microseconds, then there should be an easy roundtrip
>> method to get the value back; currently there is not.
>>
>> Maybe the best fix is to make the csv module smarter about how it
>> outputs datetime's (so it would always emit microseconds).
>>
>> -- 
>> ~Ethan~
> 
> The first solution if anything please, the latter is the road to hell.
> "You've done it for datetime objects, so why not xyz?".  "A similar
> thing was done to the csv module, so why not the ijk module?".

But since csv calls str(value), and __str__ can't be changed due to
backward compatibility constraints, nothing can be done, right?

Eric.




More information about the Python-ideas mailing list