[Python-Dev] index (was str with base)

Guido van Rossum guido at python.org
Tue Jan 17 16:26:57 CET 2006


On 1/17/06, Jim Jewett <jimjjewett at gmail.com> wrote:
> Guido wrote:
>
> > more important to implement __index__() in Python 2.5.
> > This behaves like __int__() for integral types, but is not
> > defined for float or Decimal.
>
> Why not for Decimal, or even float?  I would not be surprised
> if 10.798 failed, but I would expect 1000D to work.
>
> If indexing worked with more arbitrary extension integers,
> then I would expect it work with Decimal, and possibly float,
> when the number == a whole number, and to raise ValueError
> otherwise.

Sorry, I forgot to explain this. There's a very good reason why this
should not be allowed.

You don't know if the 1000D was the result of an exact or of an
approximate calculation. It could be a pure coincidence that it's
1000D instead of 999.9D; and we certainly don't want the latter to be
allowed for indexing. Floating point (including Decimal floating
point) calculations are fraught with uncertainties about the precision
/ accuracy of the outcome. Even if when you test your algorithm you
always get exact outcomes, there's no guarantee that with real input
data the same will hold.

Requiring the index to be an int by *type* solves this by forcing you
to think about rounding/truncating. I talked to some Numeric folks and
they understand and agree.

--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list