[Python-Dev] Example for PEP 343
Bob Ippolito
bob at redivi.com
Wed May 18 03:10:52 CEST 2005
On May 17, 2005, at 9:02 PM, Raymond Hettinger wrote:
>>> What's the advantage of using two calls to getcontext() vs. saving
>>>
> the
>
>>> context in a local variable?
>>>
>>
>> I also prefer saving the context in a local variable but that is just
>>
> a
>
>> micro-optimization. The presentation with multiple calls to
>> getcontext() was kept just to match the style of the original -- the
>> important change was the absolute save and restore versus the
>> original
>> relative adjust up and adjust down.
>>
>
> One more thought: Rather than just saving the precision, it is likely
> wiser, safer, and more general to just save and restore the whole
> context and let the wrapped block only work with a copy.
>
> oldcontext = decimal.getcontext()
> newcontext = oldcontext.copy()
> newcontext.prec += 2
> yield None
> decimal.setcontext(oldcontext)
>
> This approach defends against various kinds of unruly behavior by the
> yield target.
I think you're missing a decimal.setcontext(newcontext) before the
yield..
-bob
More information about the Python-Dev
mailing list