Your non-listcomp code works as written (kinda surprised by that), defining attributes of test, so just use that if it's what you need (it's almost certainly a bad idea, but you do you).
This doesn't work in your listcomp code because:
- testdoesn't exist until the class definition completes (after you dedent at the end), so- test.ANYTHINGdoesn't work inside the definition of- test(you assign to top-level names, and they become attributes of- testwhen the class finishes being defined, but they're just scoped names without- test.qualification before then)
- Omitting testdoesn't work in Python 3 because listcomps have their own scope in Python 3; even if you fake it out to assign to each of your names in turn, the iteration variables don't leak to outer scope on Python 3.
In any event, what you're doing is nonsensical:
- Your plain forloop code works, but
- It's a bad idea; just have a single class attribute of the listand have users index it as needed. There's basically never a good reason to have a programmatically generated list of numbered variable names. That's reinventinglists, badly.
So just use:
class test:
    a = range(0, 10, 2)  # Or list(range(0, 10, 2)) if it must be mutable
and user's can do test.a[0], test.a[3], whatever they need.