Add a variant of the decimal example.

This commit is contained in:
Guido van Rossum 2005-05-18 02:53:26 +00:00
parent b99aa09cf2
commit 2dcf31c84a
1 changed files with 14 additions and 0 deletions

View File

@ -416,6 +416,20 @@ Examples
s += num / fact * sign
return +s
An alternative version of the generator saves the entire
context instead of just the precision attribute. This is more
robust, but also more expensive, making it perhaps the better
choice when several attributes are modified together:
@do_template
def with_extra_precision(places=2):
oldcontext = decimal.getcontext()
newcontext = oldcontext.copy()
newcontext.prec += places
decimal.setcontext(newcontext)
yield None
decimal.setcontext(oldcontext)
References
[1] http://blogs.msdn.com/oldnewthing/archive/2005/01/06/347666.aspx