[Python-ideas] except expression

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Feb 17 23:56:24 CET 2014

Chris Angelico wrote:
> Maybe this is a good opportunity to introduce the concept of
> "sub-scopes" for specific constructs. Comprehensions and 'as' clauses
> (I doubt anyone will object to the statement try/except being brought
> in line with this) could then use a sub-scope inside the existing
> function; it'd affect only the LOAD_FAST opcode, I think - assignment
> could delete it from the sub-scope and put it into the main scope.

It *should* be possible to handle this entirely at
compile time. Conceptually, all you need to do is
rename the names in the subscope so that they
don't clash with anything in the outer scope, and
then generate bytecode as usual.

I haven't studied the compiler closely enough to
tell how difficult this would be to achieve, though.
Whoever implemented inner scopes for list comprehensions
reportedly found it harder than he liked at the time.


More information about the Python-ideas mailing list