scope of generators, class variables, resulting in global na

Nomen Nescio nobody at dizum.com
Wed Feb 24 06:52:50 EST 2010


Hello,

Can someone help me understand what is wrong with this example?

class T:
  A = range(2)
  B = range(4)
  s = sum(i*j for i in A for j in B)

It produces the exception:

<type 'exceptions.NameError'>: global name 'j' is not defined

The exception above is especially confusing since the following similar example (I just replaced the generator by an explicit array) works:

class T:
  A = range(2)
  B = range(4)
  s = sum([(i*j) for i in A for j in B])

(BTW, the class scope declarations are intentional).

Thanks, Leo.




More information about the Python-list mailing list