[Tutor] list comprehensions isolate variables but for loops don't - is there a special usage?
Tim Golden
mail at timgolden.me.uk
Wed Jun 5 09:44:53 CEST 2013
On 05/06/2013 08:35, Alan Gauld wrote:
> To be honest I didn't realize that comprehensions (and I assume
> generators in general) created their own local namespace.
In 2.x list comps "leak" their iteration variables while gen comps
don't. In 3.x neither does.
It's never bothered me very much either way, but the change in 3.x does
at least mean that the list comp [i for i in ...] is now, as you might
have expected, equivalent to list(i for i in ...)
TJG
More information about the Tutor
mailing list