45

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?

2
  • 1
    Well, @DeepSpace, I read the docs but all was written was that Base class for creating enumerated constants that are also subclasses of int.. This doesn't tell you why should you use one over the other, even thought one is more suitable for me. I found the use case and the explanation of @mehrdad-pedramfar very helpful. Commented Oct 22, 2018 at 15:34
  • One benefit is IntEnum allows sorting by default, Enum does not by default. Commented May 25, 2021 at 10:02

3 Answers 3

58

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'
Sign up to request clarification or add additional context in comments.

3 Comments

Well, if you already read the docs for the OP, perhaps the most obvious example for a difference between Enum and IntEnum will be that Color.RED < Color.GREEN will raise a TypeError while Shape.CIRCLE < Shape.SQUARE will not.
@DeepSpace Sure, I add the last part that said they act like integers for the same purpose. and there is a couple of more examples that we can explain here. thanks for saying, man.
I see IntEnum as glorified Int that can be used when a meaningful name is desired instead of a magic number. Much like public static final int CIRCLE = 1 in Java.
9

IntEnum is used to ensure 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'

1 Comment

I wouldn't consider this the primary difference. Ensuring that the member values are integers is a constraint rather than a feature, because IntEnum provides direct comparison with integers (that Enum doesn't do, you have to access the .value property). This can only happen properly if the values are integers, so the class checks this for you.
3

intEnum give the following advantages:

  1. It ensures the members must be integer:

    ValueError: invalid literal for int() with base 10
    

    will be raise if this is not satisfied.

  2. 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
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.