Proposal: Decimal literals in Python.

Raymond Hettinger python at rcn.com
Sat Oct 27 03:14:48 EDT 2007


On Oct 26, 1:54 am, Lennart Benschop <lenna... at xs4all.nl> wrote:
> My proposal:
> - Any decimal constant suffixed with the letter "D" or "d" will be
>   interpreted as a literal of the Decimal type. This also goes for
>   decimal constants with exponential notation.

There's nothing new here that hasn't already been proposed and
discussed on python-dev.  There were no major objections to the idea;
however, it will need to wait until there is a good C implementation
of the decimal module (which is in the works but coming along very,
very slowly).  Also, once we have a C coded decimal object, further
work would be needed to make it integrate well with the rest of the
language (i.e. making sure that everything allows numeric inputs can
handle a decimal object as a possible input).

FWIW, using the decimal module is not at all as onerous as the OP
makes it sound.  I write:

   from decimal import Decimal as D
   print D(1) / D(7) + D('0.123456')

That isn't much of a burden compared with:

    print 1d / 7d + 0.123456d

You would still need to import decimal so you can set the context
parameters (like precision and rounding).

Also, most non-toy scripts have *very* few literals in them; instead,
the decimal values arise from calculations, user inputs, and file of
data.  Casting those to the correct type is really no more difficult
that it is with other types:

  s = raw_input('Input temperature')
  print int(s), Decimal(s), float(s)

Raymond





More information about the Python-list mailing list