[Python-Dev] PEP 327: Decimal Data Type

Michael Chermside mcherm at mcherm.com
Mon Feb 2 15:42:12 EST 2004

I wrote:
>     (a) What's the syntax for changing the context? I'd 
>  think we'd want 
>  a "pushDecimalContext()" and "popDecimalContext()" sort of 
>  approach, since most 
>  well-behaved routines will want to restore their caller's context. 

Facundo Batista replies:
> You can save a copy of the context in a variable and then
> restore it. No stack.

Imagine the following senario. I write some code which uses
Decimal, and which sets the context to get the desired rounding
behavior. Somewhere in my code I call a routine written by
somebody else... perhaps some library code... which also does
some calculations using Decimal.

It would be a major pain if every time I called that library code
it changed the context. I'd have to keep setting the context back
after each and every call to the library code. A well-behaved
library will not require me to go through these contortions, so
after it does its work it will set the context back to what it was
when I called it.

It seems to me that in almost all situations where one changes
the context, one will also want to restore it later. If so, then
it seems perverse to tell every user that they must capture and
store the old context someplace and restore it later. Why not make
the best practice be the *obvious* idiom by providing matched
pushContext() and popContext() functions as the normal way of
setting the context. Of course, one could choose to call 
pushContext() without a matching popContext(), or call popContext()
repeatedly until the stack is empty (some exception is raised),
but if the best behavior is the easy behavior then most people will
use it.

And if that didn't convince you, then go ahead... I think it's only
a minor detail.

-- Michael Chermside

More information about the Python-Dev mailing list