3

I am using the colorama module and I want to be able to call the Fore on a variable corresponding to a color, for example 'GREEN'. I want to be able to do:

from colorama import Fore
color = 'GREEN'
print(Fore. + color)

I wanted it to just run print(Fore.GREEN), but I can't because its a syntax error. Is there any way to do that?

happydave
  • 7,127
  • 1
  • 26
  • 25
MagicWig
  • 43
  • 4

1 Answers1

5

You are describing the basic usage of the built-in function getattr:

>>> getattr(Fore, "GREEN")
'\x1b[32m'

This is useful when you have the name of an attribute stored in a variable.

wim
  • 338,267
  • 99
  • 616
  • 750