[Python-ideas] Assignments in list/generator expressions
Guido van Rossum
guido at python.org
Tue Apr 12 19:19:03 CEST 2011
On Tue, Apr 12, 2011 at 10:03 AM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> On Tue, Apr 12, 2011 at 12:35 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> ..
>> I am puzzled why namespace sanitation fans are so allergic to the namespace
>> cleanup statement == 'del'.
>
> Because using cleanup may unexpectedly clobber the variables you want
> to keep. When using
>
> for var in [..]:
> ..
> del var
>
> idiom, I tend to maintain some naming convention for what var can be
> so that it does not conflict with other global variables. Naming
> conventions are usually considered suboptimal work-arounds for
> languages with poor namespace support.
Also note that if that loop happened to be an empty loop, var would
not be set, and del var would fail.
Another reason why del is suboptimal is that if you have multiple
exits from your block you need a try/finally just to delete the
variables, but then you definitely need to deal with the possibility
of the variable not yet existing. Either way it's a mess.
--
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas
mailing list