Suppose it is the following structure given:
from django.utils.translation import ugettext_lazy as _
# Constants for all available difficulty types.
SIMPLE = 1
MEDIUM = 2
DIFFICULT = 3
# Names for all available difficulty types.
DIFFICULTIES = (
(SIMPLE, _("simple")),
(MEDIUM, _("medium")),
(DIFFICULT, _("difficult")),
)
How do you get the string value, if a constant is given?
A loop is easy to program, but is there a shorter python-like way with a single expression?
The expression
DIFFICULTIES[SIMPLE][1]
returns the string “medium”. What is obviously wrong.
So exchange it. (I’m assuming you kept the receipt..)
Searching through an unsorted tuple for something simply isn’t the right way to go about things. I suppose you could do
if you wanted to avoid a for loop with a colon, but that’s a little silly.