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.