Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
21b9f6b1
Commit
21b9f6b1
authored
Sep 02, 2006
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix documentation nits for decimal context managers.
parent
45349b73
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
16 deletions
+11
-16
Doc/lib/libdecimal.tex
Doc/lib/libdecimal.tex
+5
-9
Doc/whatsnew/whatsnew25.tex
Doc/whatsnew/whatsnew25.tex
+6
-7
No files found.
Doc/lib/libdecimal.tex
View file @
21b9f6b1
...
...
@@ -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
...
...
Doc/whatsnew/whatsnew25.tex
View file @
21b9f6b1
...
...
@@ -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
v
1 = decimal.
Decimal('578')
print v
1
.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 v
1
.sqrt()
print v.sqrt()
\end{verbatim}
\subsection
{
Writing Context Managers
\label
{
context-managers
}}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment