Python 3 removes name binding from outer scope

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jul 25 03:34:07 EDT 2017


On Tue, 25 Jul 2017 17:02:48 +1000, Ben Finney wrote:

> Ben Finney <ben+python at benfinney.id.au> writes:
> 
>> Having to make another name for the same object, merely to avoid some
>> surprising behaviour, is IMO un-Pythonic.
> 
> I suppose my objection is rooted in the fact this behaviour is implicit;
> my code has not issued a ‘del’ statement, and so I don't expect one; yet
> it occurs implicitly. This violates the Zen of Python.


Technically, *all* garbage collection is implicit. You don't have to 
explicitly delete your local variables when you return from a function, 
they are implicitly deleted when they go out of scope.

So consider this as a de-facto "the except clause "as name" variable is 
treated *as if* it exists in its own scope.

I agree that the behaviour of except is a little surprising, but that's 
(according to the core devs) the lesser of two evils. The alternative is 
a memory leak when the traceback keeps data alive that you didn't expect.



-- 
“You are deluded if you think software engineers who can't write 
operating systems or applications without security holes, can write 
virtualization layers without security holes.” —Theo de Raadt



More information about the Python-list mailing list