Difference between Enum and IntEnum in Python
Question:
I came across a code that looked like this:
class State(IntEnum):
READY = 1
IN_PROGRESS = 2
FINISHED = 3
FAILED = 4
and I came to the conclusion that this State class could inherit the Enum
class in the same way.
What does inheriting from IntEnum
gives me that inheriting from the regular class Enum
won’t? What is the difference between them?
Answers:
From the python Docs:
Enum: Base class for creating enumerated constants.
and:
IntEnum: Base class for creating enumerated constants that are also subclasses of int.
it says that members of an IntEnum
can be compared to integers; by extension, integer enumerations of different types can also be compared to each other.
look at the below example:
class Shape(IntEnum):
CIRCLE = 1
SQUARE = 2
class Color(Enum):
RED = 1
GREEN = 2
Shape.CIRCLE == Color.RED
>> False
Shape.CIRCLE == 1
>>True
and they will behave same as an integer:
['a', 'b', 'c'][Shape.CIRCLE]
>> 'b'
IntEnum is used to insure that members must be integer i.e.
class State(IntEnum):
READY = 'a'
IN_PROGRESS = 'b'
FINISHED = 'c'
FAILED = 'd'
This will raise an exception:
ValueError: invalid literal for int() with base 10: 'a'
intEnum
give the following advantages:
-
It ensures the members must be integer:
ValueError: invalid literal for int() with base 10
will be raise if this is not satisfied.
-
It allows comparison with integer:
import enum
class Shape(enum.IntEnum):
CIRCLE = 1
SQUARE = 2
class Color(enum.Enum):
RED = 1
GREEN = 2
print(Shape.CIRCLE == 1)
# >> True
print(Color.RED == 1)
# >> False
I came across a code that looked like this:
class State(IntEnum):
READY = 1
IN_PROGRESS = 2
FINISHED = 3
FAILED = 4
and I came to the conclusion that this State class could inherit the Enum
class in the same way.
What does inheriting from IntEnum
gives me that inheriting from the regular class Enum
won’t? What is the difference between them?
From the python Docs:
Enum: Base class for creating enumerated constants.
and:
IntEnum: Base class for creating enumerated constants that are also subclasses of int.
it says that members of an IntEnum
can be compared to integers; by extension, integer enumerations of different types can also be compared to each other.
look at the below example:
class Shape(IntEnum):
CIRCLE = 1
SQUARE = 2
class Color(Enum):
RED = 1
GREEN = 2
Shape.CIRCLE == Color.RED
>> False
Shape.CIRCLE == 1
>>True
and they will behave same as an integer:
['a', 'b', 'c'][Shape.CIRCLE]
>> 'b'
IntEnum is used to insure that members must be integer i.e.
class State(IntEnum):
READY = 'a'
IN_PROGRESS = 'b'
FINISHED = 'c'
FAILED = 'd'
This will raise an exception:
ValueError: invalid literal for int() with base 10: 'a'
intEnum
give the following advantages:
-
It ensures the members must be integer:
ValueError: invalid literal for int() with base 10
will be raise if this is not satisfied.
-
It allows comparison with integer:
import enum class Shape(enum.IntEnum): CIRCLE = 1 SQUARE = 2 class Color(enum.Enum): RED = 1 GREEN = 2 print(Shape.CIRCLE == 1) # >> True print(Color.RED == 1) # >> False