[Python-Dev] PEP 377 - allow __enter__() methods to skip the statement body

Nick Coghlan ncoghlan at gmail.com
Mon Mar 16 12:15:53 CET 2009


P.J. Eby wrote:
> At 06:28 AM 3/16/2009 +1000, Nick Coghlan wrote:
>> There are some practical hurdles to that idea (specifically, creating a
>> callable which uses its parent's namespace rather than having its own),
>> but the basic concept seems sound.
> 
> Actually, that bit's pretty simple -- they're just "nonlocal"
> variables.  But the cost of creating that function, and the cost of
> having cell variables in the surrounding function is potentially high. 
> (In CPython at least, function calls are optimized in certain ways when
> the function only has "fast" locals, and no "cell" locals.)

Some additional complexities occurred to me today - they go by the names
"return", "break" and "continue". With the current design those are
perfectly well defined inside a with statement, but they pose a pretty
serious hurdle for a practical implementation of glyph's idea.

So I'm going to stick with the more modest approach of a new control
flow exception for PEP 377.

I still find the callable block idea somewhat intriguing, but if it ever
happens it should be a new construct rather than overloading the meaning
of the with statement.

Cheers,
Nick.

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


More information about the Python-Dev mailing list