May 22, 2021

Variable Scope and List Comprehensions

Python 2 In Python 2 the temporary variable was not so temporary at all… ❯ python2 Python 2.7.17 (default, Feb 27 2021, 15:10:58) [GCC 7.5.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> s = "abc" >>> [x for x in s] ['a', 'b', 'c'] >>> x 'c' As you can see, x leaks outside the scope the list comprehension. Python 3 This has been changed in Python 3....