How do you convert enum to int in python?
I have an enum Nationality: Show
How can I convert this some enum to int in this or similar way:
I know that I can do it if I had code by:
but I don't have, and this way looks too big for python. I'm sure that there is something much simpler .
Ethan Furman 59.8k18 gold badges146 silver badges221 bronze badges asked May 19, 2011 at 14:57
user278618user278618 18.5k42 gold badges123 silver badges195 bronze badges 1 Please use IntEnum
answered Apr 3, 2018 at 9:37
3 Using either the
and in use:
The above is more easily written using
which has the added functionality of:
1 Disclosure: I am the author of the
Python stdlib answered Mar 13, 2016 at 0:02
Ethan FurmanEthan Furman 59.8k18 gold badges146 silver badges221 bronze badges 1
There are better (and more "Pythonic") ways of doing what you want. Either use a tuple (or list if it needs to be modified), where the order will be preserved:
Or use a dictionary along the lines of:
The latter is preferable, in my opinion, as it's more readable and explicit. A
per1234 8765 silver badges13 bronze badges answered May 19, 2011 at 15:07
Joe KingtonJoe Kington 264k70 gold badges590 silver badges458 bronze badges 2
Need to access tuple by index if using commas:
answered Oct 23, 2020 at 10:33
MorrisMorris 8612 gold badges9 silver badges21 bronze badges 0 Why don't you just define the values as numbers instead of strings:
If you need to access the two-letter names, you can simply provide a table that maps them. (Or a dictionary that maps the other way, etc.) answered May 19, 2011 at 15:06
andrewdskiandrewdski 5,0592 gold badges19 silver badges19 bronze badges You can't. Python does not store the order of class elements
and Seeing from your comment that you really do require a mapping from strings to integers, you should in fact do exactly that:
answered May 19, 2011 at 15:00
blubbblubb 9,1212 gold badges39 silver badges76 bronze badges 1 I have seen something like:
Wrap it in a class and viola, you have a namespace for the enumeration. answered May 19, 2011 at 15:18
janislawjanislaw 3021 silver badge7 bronze badges One straightforward way to achieve this:
Output:
Explanation: You can create your enum with the desired numeration and string representation as you wish and you get the desired functionality by
overwriting the respective answered Nov 24, 2021 at 18:23
and then:
answered Mar 19, 2020 at 8:08
and then:
also
answered Jul 15 at 3:50
AboSariAboSari 12 silver badges1 bronze badge 1 This method is something I am using
answered Sep 4 at 19:48
Thinkal VBThinkal VB 1731 silver badge10 bronze badges How do you get an integer from an enum?To get the enum for a given integer, we simply have to call the valueOf method, like below.. On the other hand, to get the integer value from an enum, one can do as follows, by using the getValue method.. The getValue method simply returns the internal value of the enum that we store within the value variable.. Can enum be integer?No, we can have only strings as elements in an enumeration.
What is int enum in Python?IntEnum() method, we can get the enumeration based on integer value, if we compare with normal enum based class it will fail by using enum. IntEnum() method. Syntax : enum.IntEnum.
How do you convert a string to an int in Python?To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed. The general syntax looks something like this: int("str") .
|