datetime.timedelta division confusion

Arnaud Delobelle arnodel at googlemail.com
Sat Sep 1 14:43:03 EDT 2007


On Sep 1, 7:28 pm, s... at pobox.com wrote:
> Running from Subversion, I see confusing (to me) behavior related to
> division of datetime.timedelta objects by integers:
>
>     % python
>     Python 2.6a0 (trunk:57277:57280M, Aug 28 2007, 17:44:49)
>     [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
>     Type "help", "copyright", "credits" or "license" for more information.
>     >>> import datetime
>     >>> d = datetime.timedelta(1)
>     >>> d / 2
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in <module>
>     TypeError: unsupported operand type(s) for /: 'datetime.timedelta' and 'int'
>     >>> d.__div__(2)
>     datetime.timedelta(0, 43200)
>
> When I run the interpreter under gdb's control with a breakpoint set in
> delta_divide, the breakpoint is not reached in the first case, but it is in
> the second.  Is there something amiss with division of timedelta objects by
> ints or am I just missing something?
>
> Skip

Try d // 2 ?
It may be that v2.6 considers / as __truediv__ and not __div__.

I can reproduce this behaviour on 2.4:

daffodil:~ arno$ python2.4
Python 2.4.3 (#1, Oct 20 2006, 12:58:47)
[GCC 4.0.1 (Apple Computer, Inc. build 5247)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import division
>>> import datetime
>>> d = datetime.timedelta(1)
>>> d/2
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for /: 'datetime.timedelta' and
'int'
>>> d // 2
datetime.timedelta(0, 43200)
>>>

HTH

--
Arnaud





More information about the Python-list mailing list