diff options
author | Raymond Hettinger <python@rcn.com> | 2006-09-02 17:07:23 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2006-09-02 17:07:23 (GMT) |
commit | 68b6d3b85aab9cd3ccd5733cd8cb6d2e16fc80a2 (patch) | |
tree | 9e74bbc431823cae1212fde411ea2e2e81365b77 /Doc | |
parent | e7877d930c44daf1b0fe22d2c33738d2c321c4b8 (diff) | |
download | cpython-68b6d3b85aab9cd3ccd5733cd8cb6d2e16fc80a2.zip cpython-68b6d3b85aab9cd3ccd5733cd8cb6d2e16fc80a2.tar.gz cpython-68b6d3b85aab9cd3ccd5733cd8cb6d2e16fc80a2.tar.bz2 |
Fix documentation nits for decimal context managers.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/lib/libdecimal.tex | 14 | ||||
-rw-r--r-- | Doc/whatsnew/whatsnew25.tex | 13 |
2 files changed, 11 insertions, 16 deletions
diff --git a/Doc/lib/libdecimal.tex b/Doc/lib/libdecimal.tex index 68748f6..b046aa3 100644 --- a/Doc/lib/libdecimal.tex +++ b/Doc/lib/libdecimal.tex @@ -448,27 +448,23 @@ active context. \begin{funcdesc}{localcontext}{\optional{c}} 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 - and restore the previous context when exiting the with statement. If + 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. If no context is specified, a copy of the current context is used. + \versionadded{2.5} - For example the following code increases the current decimal precision + For example, the following code increases the current decimal precision by 42 places, performs a calculation, and then automatically restores the previous context: \begin{verbatim} from __future__ import with_statement import decimal - with decimal.localcontext() as ctx: + with localcontext() as ctx: ctx.prec = 42 # Perform a high precision calculation s = calculate_something() s = +s # Round the final result back to the default precision \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} New contexts can also be created using the \class{Context} constructor diff --git a/Doc/whatsnew/whatsnew25.tex b/Doc/whatsnew/whatsnew25.tex index bf939c0..1c49809 100644 --- a/Doc/whatsnew/whatsnew25.tex +++ b/Doc/whatsnew/whatsnew25.tex @@ -685,20 +685,19 @@ the block is complete. The \module{decimal} module's contexts, which encapsulate the desired precision and rounding characteristics for computations, provide a -\method{context_manager()} method for getting a context manager: +\function{localcontext()} function for getting a context manager: \begin{verbatim} -import decimal +from decimal import Decimal, Context, localcontext # Displays with default precision of 28 digits -v1 = decimal.Decimal('578') -print v1.sqrt() +v = Decimal('578') +print v.sqrt() -ctx = decimal.Context(prec=16) -with ctx.context_manager(): +with localcontext(Context(prec=16)): # All code in this block uses a precision of 16 digits. # The original context is restored on exiting the block. - print v1.sqrt() + print v.sqrt() \end{verbatim} \subsection{Writing Context Managers\label{context-managers}} |