[Python-Dev] Pre-PEP: Task-local variables

Nick Coghlan ncoghlan at gmail.com
Fri Oct 21 15:34:14 CEST 2005

Phillip J. Eby wrote:
> Actually, it's fairly simple to write a generator decorator using 
> context.swap() that saves and restores the current execution state 
> around next()/send()/throw() calls, if you prefer it to be the 
> generator's responsibility to maintain such context.

Yeah, I also realised there's a fairly obvious solution to my decimal.Context 
"problem" too:

     def iter_sin(iterable):
        orig_ctx = decimal.getcontext()
        with orig_ctx as ctx:
            ctx.prec += 10
            for r in iterable:
                y = sin(r) # Very high precision during calculation
                with orig_ctx:
                    yield +y # Interim results have normal precision
                # We get "ctx" back here
        # We get "orig_ctx" back here

That is, if you want to be able to restore the original context just *save* 
the damn thing. . .

Ah well, chalk up the __suspend__/__resume__ idea up as another case of me 
getting overly enthusiastic about a complex idea without looking for simpler 
solutions first. It's not like it would be the first time ;)


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list