Add a variant of the decimal example.
This commit is contained in:
parent
b99aa09cf2
commit
2dcf31c84a
14
pep-0343.txt
14
pep-0343.txt
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue