On my version of Python (version info below), the following breaks at the line that assigns the variable e (but works just fine up until that point):
class Foo:
a = 1
b = a + 2
print(b) # works
c = [ "apple", "banana", "cherry", "dragonfruit" ]
d = [ "I like " + fruit for fruit in c]
print(d) # works
d2 = c + ["elderberry"]
print(d2) # works
e = ["I like " + c[i] for i in range(4)] #ERROR: NameError: name 'c' is not defined
print(e)
This surprises me; I can't tell whether this is intended behavior or a bug in Python. If this is intentional, can someone explain the intent?
From what I've seen, the problem only occurs in the context of static class variables. In other words, of course, if I replace "class Foo" with "if True" then the whole thing works without an error.
I'm using the following version of Python on OS-X 10.9.5: Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)