Below is a partial class definition of mine:
class Trial:
font = pygame.font.Font(None, font_size)
target_dic = {let: font.render(let, True, WHITE, BG) for let in list("ABCDEFGHJKLMNPRSTUVWX")}
The last line of the partial class definition, target_dic = {let: font.render(let, True, WHITE, BG) for let in list("ABCDEFGHJKLMNPRSTUVWX") returns the error: global name ‘font’ is not defined. Fair enough.
However, I tried the following test case and got no error:
class x:
dat = 1
datlist = [dat for i in range(10)]
Why should the first case not work? Doesn’t the member font exist by the time the dictionary comprehension is reached?
Do I need to move these operations to __init__ or is it possible to define the list exactly once when the class object is created?
EDIT:
For clarity, I’d like to be able to populate the list at class object creation time to cut down on the time spent creating Trial objects.
Partial answer, as it is more to cut some wrong paths to follow.
If I take back your working sample and put a dict comprehension:
I get also this:
So it looks like dict comprehension is hiding the temporary dict use during class statement execution, but the list comprehension doesn’t.
No further information found by now in the docs about this…
Edit based on @interjay comment:
class construction not fulfilling scope norm is addressed in this post. Short story is that list comprehension are buggy in 2.x and see class members but they shouldn’t.