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
e7877d93
Commit
e7877d93
authored
Sep 02, 2006
by
Nick Coghlan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Further corrections to the decimal module context management documentation
parent
ced1218d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
14 deletions
+11
-14
Doc/lib/libdecimal.tex
Doc/lib/libdecimal.tex
+11
-14
No files found.
Doc/lib/libdecimal.tex
View file @
e7877d93
...
...
@@ -443,36 +443,33 @@ the \function{getcontext()} and \function{setcontext()} functions:
\end{funcdesc}
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
}}
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.
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
by 2 places, performs a calculation, and then automatically restores
by
4
2 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:
ctx.prec
+= 2 # add 2 more digits of precis
ion
ctx.prec
= 42 # Perform a high precision calculat
ion
s = calculate
_
something()
s = +s # Round the final result back to the default precision
\end{verbatim}
\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.
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
described below. In addition, the module provides three pre-made
...
...
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