[Python-Dev] PEP 343 and __context__()

Nick Coghlan ncoghlan at gmail.com
Sat Jan 21 03:07:45 CET 2006


Jason Orendorff wrote:
> On 1/20/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> Jason Orendorff wrote:
>>> DecimalContext has a few problems.  In code where it matters, every
>>> function you write has to worry about it. (That is, you can't just
>>> write __decimal_context__ = ... at the top of the file and be done
>>> with it, the way you can with, say, __metaclass__.)
>> No, you write "decimal.setcontext(...)" instead.
> 
> You seem to be implying these are roughly equal in convenience; I
> disagree.  Suppose I have banking.py, in which it's important to use a
> particular precision and rounding.  Now I have to put context-munging
> code in every single function that banking.py exposes.  And around
> every 'yield'.  Even with 'with', that's a lot of extra lines of code.

Ah, I understand your point better now.

> I'd much prefer to put a one-liner at the top of the file, if it were
> possible (...but I don't see how, yet).

Me neither. The best I can come up with is a "precise" decorator that takes 
care of the context munging. Which would at least reduce the boilerplate to 
"@precise" on functions and "@precise_gen" on generators.

> Again, none of this is likely to matter--unless you're interleaving
> banking and heavy scientific calculations, which I try to avoid.  So,
> not a big deal.  Thanks for the response.

No worries. I agree there's still some awkwardness, but I do think it's an 
improvement on the status quo (where we have the same problems with 
interactions between modules and generators and thread-state, but dealing with 
them can't really be factored out at all).

>>>  And
>>> DecimalContext doesn't fit in with generators.
>> It does fit actually - you simply have to remember to restore the original
>> context around any invocations of yield.
> 
> Feh!  "Fit" is to "can be made to work with a bit of effort, just
> don't forget to follow the rules" as Python is to C++.

I see what you mean. I'm sure we can rely on PJE and others to push the limits 
of with statements and provide feedback on improving their interaction with 
generators in Python 2.6 :)

(although before that we need to bring mwh's patch up to speed with the svn 
trunk. . .)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list