This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients steven.daprano
Date 2022-03-27.10:16:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1648376164.23.0.483054740742.issue47135@roundup.psfhosted.org>
In-reply-to
Content
Whenever I use decimal and need to change the context temporarily, without fail I try to write

with decimal.localcontext(prec=10):
    ...

or similar. Then I get surprised that it fails, and re-write it as

with decimal.localcontext() as ctx:
    ctx.prec = 10
    ...

Let's make the first version work. localcontext should accept keyword arguments corresponding to the same arguments accepted by Context, and set them on the context given.

A proof-of-concept wrapper function:

def localcontext(ctx=None, **kwargs):
    if ctx is None:
        ctx = decimal.getcontext().copy()
    for key, value in kwargs.items():
        setattr(ctx, key, value)
    return decimal.localcontext(ctx)

I think this would be a valuable, and useful, improvement to the decimal API.
History
Date User Action Args
2022-03-27 10:16:04steven.dapranosetrecipients: + steven.daprano
2022-03-27 10:16:04steven.dapranosetmessageid: <1648376164.23.0.483054740742.issue47135@roundup.psfhosted.org>
2022-03-27 10:16:04steven.dapranolinkissue47135 messages
2022-03-27 10:16:04steven.dapranocreate