471,336 Members | 1,265 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

datetime.timedelta division confusion


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

Sep 1 '07 #1
2 4126
On Sep 1, 7:28 pm, s...@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
Sep 1 '07 #2
Try d // 2 ?
It may be that v2.6 considers / as __truediv__ and not __div__.
Ah, right you are. Thanks...

Skip


Sep 1 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Paul Moore | last post: by
5 posts views Thread by Robert Brewer | last post: by
1 post views Thread by James | last post: by
4 posts views Thread by Lad | last post: by
4 posts views Thread by Lad | last post: by
reply views Thread by ZoombyWoof | last post: by
5 posts views Thread by mroeloffs | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.