Python 3 removes name binding from outer scope

Ian Kelly ian.g.kelly at gmail.com
Tue Jul 25 15:52:46 EDT 2017


On Tue, Jul 25, 2017 at 5:38 AM, Thomas Jollans <tjol at tjol.eu> wrote:
> On 2017-07-25 09:28, Chris Angelico wrote:
>> It actually does the equivalent of:
>>
>> finally:
>>     e = None
>
> I wonder why it would bother to load None... (as someone not very
> familiar with Python at the bytecode level)

If I may hazard a guess, it's because simply deleting the variable
will raise a NameError if the variable is not already bound, e.g. if
there was no exception and only the finally block is being executed,
or if the programmer already deleted it. Assigning None to ensure the
variable is bound is likely faster than testing it.



More information about the Python-list mailing list