Just looking at ways of getting named constants in python.
class constant_list: (A_CONSTANT, B_CONSTANT, C_CONSTANT) = range(3)
Then of course you can refer to it like so:
constant_list.A_CONSTANT
I suppose you could use a dictionary, using strings:
constant_dic = { 'A_CONSTANT' : 1, 'B_CONSTANT' : 2, 'C_CONSTANT' : 3,}
and refer to it like this:
constant_dic['A_CONSTANT']
My question, then, is simple. Is there any better ways of doing this? Not saying that these are inadequate or anything, just curious – any other common idioms that I’ve missed?
Thanks in advance.
For 2.3 or after:
To use:
codes.BAZwill be 2 and so on.If you only have 2.2, precede this with:
(This was taken from here)