dimanche 17 juillet 2016

List comprehensions leak their loop variable in Python2: how making it be compatible with Python3

I just learnt from Why do list comprehensions write to the loop variable, but generators don't? that List comprehensions also "leak" their loop variable into the surrounding scope.

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
>>> x = 'before'
>>> a = [x for x in (1, 2, 3)]
>>> x
3

This bug is fixed in Python3.

Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
>>> x = 'before'
>>> a = [x for x in (1, 2, 3)]
>>> x
'before'

What is the best way to make Python2 be compatible with Python3 at this point?

Aucun commentaire:

Enregistrer un commentaire