[Python-ideas] adding an __exec__ method to context managers?

Jim Jewett jimjjewett at gmail.com
Tue Oct 13 15:03:03 CEST 2009


On Mon, Oct 12, 2009 at 11:09 PM, Carl Johnson
<cmjohnson.mailinglist at gmail.com> wrote:
> Taking a step back, it seems like what you really want is some easy
> way to create callbacks, just like Ruby blocks or the new Objective-C
> blocks. There are a number of ways this could be done:

Hmm... I saw this as bigger than a block; it was creating a full
execution context, not just just a suite executed in a tweak of the
current context.

But I may have been reading too much into it.

Would the __exec__ have its own globals and locals (which might
default to a reference to or copy of the current ones)?

> 4. Some modification to the "with" statement, as you are proposing.
> The resistance that you will face with this idea is that it is
> significantly different from how "with" works now, since it does not
> create a block at all.

To me, "with" says:

"Take the current execution context, tweak it, run the following
suite, then untweak the context".

An __exec__ just strengthens the possible separation between the inner
and and outer contexts -- it may be slightly less efficient, but in
return, it will be a better sandbox.

OpenMP would be a special type of __exec__ that also happens to handle
parallelization for you.

-jJ



More information about the Python-ideas mailing list