How do you get a key of an enum by calling its value?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bitdev

I can’t seem to find any documentary, videos, or questions same with mine to help me.
Like nothing really…

I’m not sure that’s possible, but it’s also something I’ve never wanted / needed from any dev language. Can you describe the situation you’re considering?

If you really need to map between value and key, I’d suggest that you might be better off with a Dictionary.

jgodfrey | 2023-01-15 16:20

:bust_in_silhouette: Reply From: LeslieS

Given this:

enum test {one, two, three}      

You can use this:

print(test.keys()[1])    

…to print:

two     

However it is an unusual situation wherein you would need that functionality (as jgodfrey states) since this will also work:

print(test.keys()[test.two]    

…which proves that you already know what the key name is.

it’s exactly what i needed, thank you!

bitdev | 2023-01-18 02:17