[Python-Dev] PEP 343 and __context__()

Phillip J. Eby pje at telecommunity.com
Fri Jan 20 18:48:19 CET 2006


At 07:21 PM 01/20/2006 +1000, Nick Coghlan wrote:
>Yeah, it came up in response to PJE's suggestion of task-local variables for
>generators. The basic concept we came up with is that if you're writing a
>generator that uses a specific context, remember to save the original and
>restore it around any calls to yield (at least, I came up with the idea and
>no-one objected to the approach [1]).

That's only because Guido shut down the thread.  :)

For the use cases I had in mind, your approach simply doesn't scale, 
because it requires every yield-point to have global knowledge of all 
"changes to global state" that may have occurred somewhere above it in the 
coroutine call stack.  The only reason I didn't point this out is that 
Guido had asked for a postponement of the discussion.

Since then, I've begun implementing a library that accomodates such use 
cases, as well as a wide variety of other context-management use 
cases.  Source code is at http://svn.eby-sarna.com/Contextual/ and the docs 
are at http://svn.eby-sarna.com/Contextual/context.txt?view=markup

The library uses the current draft of PEP 343 as the basis for 
implementation, and includes a few helper functions that can be used to 
emulate the "with:" statement in Python 2.4 so that it can be used and 
tested.  So far, my impression is that the current 343 spec is at least 
adequate, and maybe even elegant.



More information about the Python-Dev mailing list