Lets say I have a class like this:
class Colors:
RED = '\33[31m'
GREEN = '\33[32m'
YELLOW = '\33[33m'
BLUE = '\33[34m'
...
How can I loop through these variables without turning them into strings?
eg:
for color in Colors:
print(color + 'foo') //should print foo in terminal in each color
I attempted to use this question's answer, but this was the result:
colors = [attr for attr in dir(Colors) if not callable(getattr(Colors, attr)) and not attr.startswith("__")]
for color in colors:
print(color + 'foo')
//prints this:
//REDfoo
//GREENfoo
...
I also tried this:
colors = [attr for attr in dir(Colors) if not callable(getattr(Colors, attr)) and not attr.startswith("__")]
for color in colors:
print(Colors.color + 'foo') //error: Colors has no attribute 'color'
print(Colors[color] + 'foo') //error: Colors object is not subscriptable
Is there any way to make this work that doesn't involve copy/pasting each color?
this is where I found the colors.