exec and locals

Chris Angelico rosuav at gmail.com
Thu Feb 27 06:41:13 EST 2014


On Thu, Feb 27, 2014 at 10:29 PM, Gregory Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Steven D'Aprano wrote:
>>
>> On Thu, 27 Feb 2014 16:34:33 +1300, Gregory Ewing wrote:
>>
>>> 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.
>
>
> More yucky than wrapping the Py3 version in an
> exec? To my way of thinking, that cancels out any
> elegance that might have been gained from using
> a with-statement.
>
> Do you really need to use the context manager
> at all? Could you just write the try-statement
> that you would have written in Py2 if you
> didn't have a context manager?

If I have to support two vastly different versions, I would prefer
(when possible) to write the code so that dropping the old version's
support is simply a matter of deleting stuff. Write the code for the
new version, then warp it as little as possible to support the old
version as well, and keep it clear which bits are for the old. Writing
code that avoids 'with' altogether goes against that.

ChrisA



More information about the Python-list mailing list