[Python-Dev] Python-Dev Digest, Vol 69, Issue 143

Steven D'Aprano steve at pearwood.info
Fri Apr 17 05:20:12 CEST 2009


On Fri, 17 Apr 2009 12:10:59 pm Tennessee Leeuwenburg wrote:
> Actually, that's a point.
>
> If its' the 31st of Jan, then +1 monthdelta will be 28 Feb and
> another +1 will be 28 March whereas 31st Jan +2 monthdeltas will be
> 31 March.
>
> That's the kind of thing which really needs to be documented, or I
> think people really will make mistakes.

It might be worth noting as an aside, but it should be obvious in the 
same way that string concatenation is different from numerical 
addition:

1 + 2 = 2 + 1
'1' + '2' != '2' + '1'


> For example, should a monthdelta include a goal-day so that the
> example above would go 31 Jan / 28 Feb / 31 March?

No, that just adds complexity.

Consider floating point addition. If you want to step through a loop 
while doing addition, you need to be aware of round-off error:

>>> x = 0.0
>>> step = 0.001
>>> for i in xrange(1000):
...     x += step
...
>>> x
1.0000000000000007

The solution isn't to add a "goal" to the plus operator, but to fix your 
code to use a better algorithm:

>>> y = 0.0
>>> for i in xrange(1, 1001):
...     y = i*step
...
>>> y
1.0

Same with monthdelta.



-- 
Steven D'Aprano


More information about the Python-Dev mailing list