擅长:python、mysql、java
<p>因为timedelta的定义如下:</p>
<pre><code>class datetime.timedelta([days,] [seconds,] [microseconds,] [milliseconds,] [minutes,] [hours,] [weeks])
</code></pre>
<p>所有参数都是可选的,默认为0。</p>
<p>你可以用这种方式用可选参数轻松地说“三天四毫秒”。</p>
<pre><code>>>> datetime.timedelta(days=3, milliseconds=4)
datetime.timedelta(3, 0, 4000)
>>> datetime.timedelta(3, 0, 0, 4) #no need for that.
datetime.timedelta(3, 0, 4000)
</code></pre>
<p>对于str casting,它返回一个很好的格式化值,而不是<code>__repr__</code>,以提高可读性。来自文档:</p>
<blockquote>
<p>str(t) Returns a string in the form [D day[s], ][H]H:MM:SS[.UUUUUU],
where D is negative for negative t. (5)</p>
</blockquote>
<pre><code>>>> datetime.timedelta(seconds = 42).__repr__()
'datetime.timedelta(0, 42)'
>>> datetime.timedelta(seconds = 42).__str__()
'0:00:42'
</code></pre>
<p>签出文档:</p>
<p><a href="http://docs.python.org/library/datetime.html#timedelta-objects" rel="noreferrer">http://docs.python.org/library/datetime.html#timedelta-objects</a></p>