Odd behaviour with list comprehension
Micah Cowan
micah at cowan.name
Fri Feb 29 23:47:12 EST 2008
"Jerry Hill" <malaclypse2 at gmail.com> writes:
> On Fri, Feb 29, 2008 at 10:01 PM, Ken Pu <kenpuca.dev at gmail.com> wrote:
>> Is there a way for me keep the iterating variable in list
>> comprehension local to the list comprehension?
>
> Kind of. You can use a generator expression instead of a list
> comprehension, and those don't leak their internal variables into the
> enclosing scope:
Whoa, that's cool. I didn't even think to try that, just assuming it
would do the same.
Though now that I think about it, I don't see how it possibly could,
since it just evaluates to an object that you could pass around, and
return, using it the same way elsewhere.
--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
More information about the Python-list
mailing list