Was sind Regeln für das Listenverständnis innerhalb einer Python-Klasse? [Duplikat

Diese Frage hat hier bereits eine Antwort:

Auf Klassenvariablen aus einem Listenverständnis in der Klassendefinition zugreifen 5 Antworten

Im folgenden Code wird dasmc Zuweisung funktioniert gut in Python 2 und 3.

Dascc -Zuweisung, die dasselbe Listenverständnis innerhalb einer Klasse verwendet, funktioniert in Python 2, schlägt jedoch mit Python 3 fehl.

Was erklärt dieses Verhalten?

ml1 = "a b c".split()
ml2 = "1 2 3".split()
mc = [ i1 + i2 for i1 in ml1 for i2 in ml2 ]

class Foo(object):
    cl1 = ml1
    cl2 = ml2

    cc1 = [ i1 for i1 in cl1 ]
    cc2 = [ i2 for i2 in cl2 ]
    cc = [ i1 + i2 for i1 in cl1 for i2 in cl2 ]


print("mc = ", mc)
foo = Foo()
print("cc = ", foo.cc)

Ich bekomme das:

(default-3.5) snafu$ python2 /tmp/z.py 
('mc = ', ['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3'])
('cc = ', ['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3'])

(default-3.5) snafu$ python3 /tmp/z.py 
Traceback (most recent call last):
  File "/tmp/z.py", line 5, in <module>
    class Foo(object):
  File "/tmp/z.py", line 11, in Foo
    cc = [ i1 + i2 for i1 in cl1 for i2 in cl2 ]
  File "/tmp/z.py", line 11, in <listcomp>
    cc = [ i1 + i2 for i1 in cl1 for i2 in cl2 ]
NameError: name 'cl2' is not defined

Warum ist die Klassenvariablecl2 nicht definiert? Notiere dass dercc2 Zuweisung funktioniert gut, ebenso wiecc1. Tauschencl1 undcl2 im Verständnis zeigt, dass die zweite Schleife diejenige ist, die die Ausnahme auslöst, nichtcl2 per se.)

Versions:

(default-3.5) snafu$ python2 --version
Python 2.7.11+
(default-3.5) snafu$ python3 --version
Python 3.5.1+

Antworten auf die Frage(2)

Ihre Antwort auf die Frage