exec and locals

Steven D'Aprano steve at pearwood.info
Thu Feb 27 05:39:26 CET 2014


On Thu, 27 Feb 2014 16:34:33 +1300, Gregory Ewing wrote:

> Steven D'Aprano wrote:
>>     except SyntaxError:
>>         def inner():
>>             # manually operate the context manager call context manager
>>             __enter__
>>             try:
>>                 try:
>>                     return something
>>                 except:  # Yes, a bare except. Catch EVERYTHING.
>>                     blah blah blah
>>             finally:
>>                     call context manager __exit__
> 
> Why not just use this version all the time? It should work in both 2.x
> and 3.x.

Because that's yucky. It's an aesthetic thing: when supported, I want the 
Python interpreter to manage the context manager.

The exec part is only half a dozen lines, only three lines of source 
code. It's no burden to keep it for the cases where it works (that is, at 
least 2.6 onwards).


-- 
Steven



More information about the Python-list mailing list