19

have a look at the following piece of code, which shows a list comprehension..

>>> i = 6
>>> s = [i * i for i in range(100)]
>>> print(i)

When you execute the code example in Python 2.6 it prints 99, but when you execute it in Python 3.x it prints 6.

What were the reason for changing the behaviour and why is the output 6 in Python 3.x?

Thank you in advance!

Joschua
  • 5,816
  • 5
  • 33
  • 44
  • 2
    Wow. I'm lucky to have found it here, I'd look for the bug for weeks if I stumbled upon this in my own code. +1 for question, -1 for Python :( – Kos Jan 01 '11 at 20:49
  • Similar question here: http://stackoverflow.com/questions/4198906/python-list-comprehension-rebind-names-even-after-scope-of-comprehension-is-this – viam0Zah May 04 '11 at 11:27

4 Answers4

33

The old behaviour was a mistake but couldn't easily be fixed as some code relied on it.

The variable i inside the list comprehension should be a different i from the one at the top level. Logically it should have its own scope which does not extend outside the comprehension as its value only makes sense inside the comprehension. But in Python 2.x due to an implementation detail the scope was larger than necessary causing the variable to "leak" into the outer scope, causing the confusing results you see.

Python 3.0 was deliberately not intended to be backwards compatible with previous releases, so they used the opportunity to fix this undesirable behaviour.

In Python 2.3 and later releases, a list comprehension “leaks” the control variables of each for it contains into the containing scope. However, this behavior is deprecated, and relying on it will not work in Python 3.0

Source

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
7

Yes, there is a reason, and the reason is that they didn't want the temporary variable in a list comprehension to leak into the outer namespace. So it is an intentional change that is a result of list comprehensions now being syntactic sugar for passing a generator expression to list().

Ref: PEP3100.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
5

Mark Byers answered it perfectly.

just as a side note..
in Python 2.x, if you change your brackets to parens (creating a generator expression instead of a list comprehension), you will notice that the control variable is not leaked.

>>> i = 6
>>> s = (i for i in range(100))
>>> print i
6

vs.

>>> i = 6
>>> s = [i for i in range(100)]
>>> print i
99

(of course, in Python 3, this is fixed and list comprehensions no longer leak control variables)

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
  • That's a good point. But it should also be noted that `s` also does not now contain a list, so to get the desired final result you must do `list(s)`. – Keith Jan 01 '11 at 23:59
0

Looks like a change in scoping to me.

I confirmed your result in Python 2.6; it does indeed print 99, which is the last value assigned to i in the list comprehension.

duffymo
  • 305,152
  • 44
  • 369
  • 561