prePEP: Decimal data type
Jp Calderone
exarkun at intarweb.us
Thu Nov 6 16:09:16 EST 2003
On Thu, Nov 06, 2003 at 03:37:52PM -0500, Aahz wrote:
> In article <vqlaif4imh9k98 at news.supernews.com>,
> John Roth <newsgroups at jhrothjr.com> wrote:
> >
> >AFAICS, there are only two solutions:
> >
> ><decimal>.add(<number>, [<result spec>])
> >
> >and
> >
> ><context>.add(<decimal>, <number>)
>
> Both are too inconvenient for casual users. I think that for the Money
> subclass, it'll probably make sense to require users to specify an
> explicit Context before the first operation (raising an exception if not
> initialized). For Decimal, using the same defaults as Rexx will
> probably work just fine.
That sounds like it would work, if you are forced to specify the context
on a per-instance basis. Otherwise, I don't see how two different modules
which wanted to treat Money differently could ever be used together.
How about this instead?
import context
class Decimal:
ROUNDING_STYLE = 'decimal-rounding-style'
ITALIAN, FRENCH, AMERICAN = range(3)
def __add__(self, other):
rstyle = context.get(Decimal.ROUNDING_STYLE, 'sane default')
# Do stuff, depending on rstyle
return stuff
def jiggerSomeMoneys(a, b, c):
# Lots of operations on a, b, and c
context.call({Decimal.ROUNDING_STYLE: Decimal.ITALIAN}, jiggerSomeMoneys, a, b, c)
This way, only the top-most invocation need deal with the context. The
actual implementation dealing with decimals is free to ignore it completely,
and should it require a change, there is only a single place where that
change is required.
As an added bonus, the context code is completely independent of the
decimal code, and can be re-used for any other application requiring
contextual data.
Implementations of context.call and context.get are left as an exercise to
the reader (Eh, ok, I can show you mine if anyone is actually interested :)
Jp
More information about the Python-list
mailing list