diff options
author | Sam Ezeh <sam.z.ezeh@gmail.com> | 2022-04-22 04:27:15 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-22 04:27:15 (GMT) |
commit | bcf14ae4336fced718c00edc34b9191c2b48525a (patch) | |
tree | 7067583bc8a296b111e8858286b4b1d00bad97d1 /Doc/library/decimal.rst | |
parent | 5e130a8da4e4f13444ec20dfe88a3e2e070005ca (diff) | |
download | cpython-bcf14ae4336fced718c00edc34b9191c2b48525a.zip cpython-bcf14ae4336fced718c00edc34b9191c2b48525a.tar.gz cpython-bcf14ae4336fced718c00edc34b9191c2b48525a.tar.bz2 |
gh-91291: Accept attributes as keyword arguments in decimal.localcontext (#32242)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Doc/library/decimal.rst')
-rw-r--r-- | Doc/library/decimal.rst | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/Doc/library/decimal.rst b/Doc/library/decimal.rst index e759c5c..2ad84f2 100644 --- a/Doc/library/decimal.rst +++ b/Doc/library/decimal.rst @@ -925,12 +925,13 @@ Each thread has its own current context which is accessed or changed using the You can also use the :keyword:`with` statement and the :func:`localcontext` function to temporarily change the active context. -.. function:: localcontext(ctx=None) +.. function:: localcontext(ctx=None, \*\*kwargs) Return a context manager that will set the current context for the active thread to a copy of *ctx* on entry to the with-statement and restore the previous context when exiting the with-statement. If no context is specified, a copy of the - current context is used. + current context is used. The *kwargs* argument is used to set the attributes + of the new context. For example, the following code sets the current decimal precision to 42 places, performs a calculation, and then automatically restores the previous context:: @@ -942,6 +943,21 @@ function to temporarily change the active context. s = calculate_something() s = +s # Round the final result back to the default precision + Using keyword arguments, the code would be the following:: + + from decimal import localcontext + + with localcontext(prec=42) as ctx: + s = calculate_something() + s = +s + + Raises :exc:`TypeError` if *kwargs* supplies an attribute that :class:`Context` doesn't + support. Raises either :exc:`TypeError` or :exc:`ValueError` if *kwargs* supplies an + invalid value for an attribute. + + .. versionchanged:: 3.11 + :meth:`localcontext` now supports setting context attributes through the use of keyword arguments. + New contexts can also be created using the :class:`Context` constructor described below. In addition, the module provides three pre-made contexts: |