mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Further corrections to the decimal module context management documentation
This commit is contained in:
parent
ced1218dd1
commit
e7877d930c
1 changed files with 11 additions and 14 deletions
|
@ -443,37 +443,34 @@ the \function{getcontext()} and \function{setcontext()} functions:
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
Beginning with Python 2.5, you can also use the \keyword{with} statement
|
Beginning with Python 2.5, you can also use the \keyword{with} statement
|
||||||
to temporarily change the active context.
|
and the \function{localcontext()} function to temporarily change the
|
||||||
|
active context.
|
||||||
|
|
||||||
\begin{funcdesc}{localcontext}{\optional{c}}
|
\begin{funcdesc}{localcontext}{\optional{c}}
|
||||||
Return a context manager that will set the current context for
|
Return a context manager that will set the current context for
|
||||||
the active thread to a copy of \var{c} on entry to the with statement
|
the active thread to a copy of \var{c} on entry to the with statement
|
||||||
and restore the previous context when exiting 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.
|
||||||
|
|
||||||
For example the following code increases the current decimal precision
|
For example the following code increases the current decimal precision
|
||||||
by 2 places, performs a calculation, and then automatically restores
|
by 42 places, performs a calculation, and then automatically restores
|
||||||
the previous context:
|
the previous context:
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
import decimal
|
import decimal
|
||||||
|
|
||||||
with decimal.localcontext() as ctx:
|
with decimal.localcontext() as ctx:
|
||||||
ctx.prec += 2 # add 2 more digits of precision
|
ctx.prec = 42 # Perform a high precision calculation
|
||||||
s = calculate_something()
|
s = calculate_something()
|
||||||
s = +s # Round the final result back to the default precision
|
s = +s # Round the final result back to the default precision
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
|
The context that is held by the context manager and made active in the
|
||||||
|
body of the \keyword{with} statement is a \emph{copy} of the context
|
||||||
|
you provide to this function, so modifying its attributes doesn't
|
||||||
|
affect anything except that temporary copy.
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
The context that's active in the body of the \keyword{with} statement is
|
|
||||||
a \emph{copy} of the context you provided to the \keyword{with}
|
|
||||||
statement, so modifying its attributes doesn't affect anything except
|
|
||||||
that temporary copy.
|
|
||||||
|
|
||||||
You can use any decimal context in a \keyword{with} statement, but if
|
|
||||||
you just want to make a temporary change to some aspect of the current
|
|
||||||
context, it's easiest to just use \function{getcontext()} as shown
|
|
||||||
above.
|
|
||||||
|
|
||||||
New contexts can also be created using the \class{Context} constructor
|
New contexts can also be created using the \class{Context} constructor
|
||||||
described below. In addition, the module provides three pre-made
|
described below. In addition, the module provides three pre-made
|
||||||
contexts:
|
contexts:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue