Message169266
Nick Coghlan <report@bugs.python.org> wrote:
> Any third party Decimal manipulating function that accepts an
> optional context and passes it down to the standard Decimal API
> will be confronted with the same problem in 3.3: passing None
> as the context no longer means the same thing as omitting the
> context argument entirely.
I agree, but for me the issue is: What is the standard API?
If you look at the Context methods in decimal.py, you see this pattern:
def add(self, a, b):
...
r = a.__add__(b, context=self)
So I think it's reasonably safe to say that all Decimal methods only
take a context=None argument because it happens to be a convenient way
of implementing the Context methods.
With that reasoning, most of the list in msg169144 would be eliminated
already. I sort of regret that the Decimal methods of the C version take
a context argument at all, but the arguments are documented.
Now to localcontext(ctx=None). Yes, code might exist that does the
following:
def f(a, b, context=None):
with localcontext(context):
return a / b
It is, however, a strange function: If I explicitly pass a context to
a function, I'd expect that it is also used for recording any status
that accumulates in the function (or that the function actually *can*
accumulate status at all).
If I'm only interested in the precision, I'd write:
def f(a, b, prec=None):
with localcontext() as c:
c.prec = 9 if prec is None else prec
return Decimal(a) / b
[This is along the lines of Raymond's original suggestion in #15136.]
But there are other examples of unexpected behavior, such as
Decimal.to_eng_string(context) taking a context purely to determine
the value of context.capitals, i.e. no status flags can possibly be
set. Here I'd also prefer:
to_eng_string(capitals=1) |
|
Date |
User |
Action |
Args |
2012-08-28 10:28:42 | skrah | set | recipients:
+ skrah, georg.brandl, rhettinger, mark.dickinson, ncoghlan, ezio.melotti, Arfrever, zach.ware |
2012-08-28 10:28:42 | skrah | set | messageid: <1346149722.44.0.194261819778.issue15783@psf.upfronthosting.co.za> |
2012-08-28 10:28:41 | skrah | link | issue15783 messages |
2012-08-28 10:28:41 | skrah | create | |
|