extend methods of decimal module

Chris Angelico rosuav at gmail.com
Fri Feb 28 06:00:10 CET 2014

On Fri, Feb 28, 2014 at 3:41 PM, Mark H. Harris <harrismh777 at gmail.com> wrote:
> So, I am thinking I need to mods...   maybe an idmath.py for interactive sessions, and then dmath.py for for running within my scientific scripts...  ??

No; the solution is to put quotes around your literals in interactive
mode, too. There's no difference between interactive and script mode,
and adding magic to interactive mode will only cause confusion.

Alternatively, there is another solution that's been posited from time
to time: Decimal literals. We currently have three forms of numeric
literal, which create three different types of object:

>>> type(1)
<class 'int'>
>>> type(.1)
<class 'float'>
>>> type(1j)
<class 'complex'>

If we had some other tag, like 'd', we could actually construct a
Decimal straight from the source code. Since source code is a string,
it'll be constructed from that string, and it'll never go via float.
Something like this:

>>> type(0.1d)
<class 'decimal.Decimal'>

which currently is a SyntaxError, so it wouldn't collide with
anything. The question is how far Python wants to bless the Decimal
type with syntax - after all, if Decimal can get a literal notation,
why can't Fraction, and why can't all sorts of other types? And that's
a huge can of worms.


More information about the Python-list mailing list