comprehensions was Re: Switch statements again

Bengt Richter bokr at oz.net
Fri Jan 17 11:26:16 EST 2003


On Fri, 17 Jan 2003 04:05:09 -0800, Erik Max Francis <max at alcyone.com> wrote:

>Ville Vainio wrote:
>
>> Of course, there is still the embarrassing issue of the list
>> comprehension variable not being local to the list comprehension. Why,
>> oh why won't people pre-deprecate it... at least in the documentation.
>> Any hope to get it into the tutorial/lang reference for 2.3 :)?
>
>Probably for the same reason that other iterative constructs that
>introduce variables don't make them local to that structure either:
>
>>>> [x for x in range(10)]
>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>> x
>9
>>>> for y in range(10): print y,
>... 
>0 1 2 3 4 5 6 7 8 9
>>>> y
>9
>
>If one annoys you, surely the other would, too?

I think the case is stronger for making list comprehension variables
have their own scope, because it is an expression, whereas for ... is a
statement. If statement suites of some kind got their own local scope,
then for variables should probably be local to the suite block, but we
don't have that.

<try:>

List comprehensions shoot stray name-bullets into the current name crowd,
and IMO that's bad for an expression to do.

<except ExcessiveMetaphorElaborationError:>
   <pass ;-)>

Regards,
Bengt Richter




More information about the Python-list mailing list