[Python-Dev] Adventures with Decimal

Aahz aahz at pythoncraft.com
Sat May 21 07:24:58 CEST 2005


On Fri, May 20, 2005, Raymond Hettinger wrote:
>
> k.) The biggest client of all these methods is the Decimal module
> itself.  Throughout the implementation, the code calls the Decimal
> constructor to create intermediate values.  Every one of those calls
> would need to be changed to specify a context.  Some of those cases are
> not trivially changed (for instance, the hash method doesn't have a
> context but it needs to check to see if a decimal value is exactly an
> integer so it can hash to that value).  Likewise, how do you use a
> decimal value for a dictionary key when the equality check is context
> dependent (change precision and lose the ability to reference an entry)?

I'm not sure this is true, and if it is true, I think the Decimal module
is poorly implemented.  There are two uses for the Decimal() constructor:

* copy constructor for an existing Decimal instance (or passing in a
tuple directly to mimic the barebones internal)

* conversion constructor for other types, such as string

Are you claiming that the intermediate values are being constructed as
strings and then converted back to Decimal objects?  Is there something
else I'm missing?  I don't think Tim is claiming that the copy
constructor needs to obey context, just string conversions.

Note that comparison is not context-dependent, because context only
applies to results of operations, and the spec's comparison operator
(equivalent to cmp()) only returns (-1,0,1) -- guaranteed to be within
the precision of any context.  ;-)

Note that hashing is not part of the standard, so whatever makes most
sense in a Pythonic context would be appropriate.  It's perfectly
reasonable for Decimal's __int__ method to be unbounded because Python
ints are unbounded.

All these caveats aside, I don't have a strong opinion about what we
should do.  Overall, my sentiments are with Tim that we should fix this,
but my suspicion is that it probably doesn't matter much.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"The only problem with Microsoft is they just have no taste." --Steve Jobs


More information about the Python-Dev mailing list