[Python-Dev] set/dict comprehensions don't leak in Py2.7 - intentional? (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Mon Jul 5 16:25:52 CEST 2010


On Mon, Jul 5, 2010 at 11:12 PM, Tim Golden <mail at timgolden.me.uk> wrote:

I had understood (without being able to put my finger on a relevant thread) that all comprehensions were going not leak their loop variables in the future.

This understanding is correct (with that future being Python 3.x where all comprehensions have their own private scope).

As others have noted, generator expressions in 2.x already didn't leak the iteration variable into the surrounding scope.

The only reason list comprehensions continue to leak the iteration variable in 2.6 and 2.7 is that there is code out there that relies on that behaviour. However, since legacy code can't include set or dict comprehensions, there was no need for the backported versions to replicate this misfeature of 2.x list comprehensions.

Cheers, Nick.

-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia



More information about the Python-Dev mailing list