I have two enums NAME
and ALIAS
which are guaranteed to have the same number of constants, and I need a way to convert each constant from NAME
to its corresponding one from ALIAS
, and vice-versa. For example:
def name_to_alias(name):
if name == Name.PAUL:
return Alias.STARCHILD
elif name == Name.GENE:
return Alias.DEMON
...
def alias_to_name(alias):
if alias == Alias.STARCHILD:
return Name.PAUL
elif alias == Alias.DEMON:
return Name.GENE
...
I don’t wish to maintain two functions (or dicts) like these. Ideally, I’d have the enum mappings in a single data structure which I can access from both conversion functions.
I’m thinking of something like:
mappings = {
Name.PAUL: Alias.STARCHILD
Name.GENE: Alias.DEMON
...
}
This would work for converting from Name
to Alias
, but the opposite may have issues (what happens if I accidentally have two dict keys with the same value?). Is there an easy and safe way to achieve this?