This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add ordering between `Enum` objects
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: barry, cool-RR, ethan.furman, pitrou
Priority: normal Keywords:

Created on 2014-09-27 14:28 by cool-RR, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (8)
msg227678 - (view) Author: Ram Rachum (cool-RR) * Date: 2014-09-27 14:28
I suggest making Enum members orderable, according to their order in the enum type. Currently trying to order them raises an exception:

    >>> import enum
    >>> class Number(enum.Enum):
    ...     one = 1
    ...     two = 2
    ...     three = 3
    >>> sorted((Number.one, Number.two))
    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        sorted((Number.one, Number.two))
    TypeError: unorderable types: Number() < Number()

If there's agreement from core developers that this is a good feature to add, I'll write a patch.
msg227683 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2014-09-27 14:59
On Sep 27, 2014, at 02:28 PM, Ram Rachum wrote:

>I suggest making Enum members orderable, according to their order in the enum
>type.

Can you please provide a motivating use case?
msg227685 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-09-27 15:31
Why don't you use IntEnum?
msg227686 - (view) Author: Ram Rachum (cool-RR) * Date: 2014-09-27 15:35
Just because I want to be able to get the `int` value of an enum object, doesn't mean I want the enum object to *be* an `int`, which is what `IntEnum` means. I don't want it to be comparable to an int, I don't want to use arithmetic on it, and most importantly I don't want it to be equal to an enum object of a different type that happens to have the same int.
msg227688 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2014-09-27 16:00
On Sep 27, 2014, at 03:35 PM, Ram Rachum wrote:

>Just because I want to be able to get the `int` value of an enum object,
>doesn't mean I want the enum object to *be* an `int`, which is what `IntEnum`
>means. I don't want it to be comparable to an int, I don't want to use
>arithmetic on it, and most importantly I don't want it to be equal to an enum
>object of a different type that happens to have the same int.

Okay, but that still doesn't explain the use case.  Also, why wouldn't an Enum
subclass, possibly using the ordered dictionary __members__ would not be
sufficient.
msg227690 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2014-09-27 16:02
https://docs.python.org/3/library/enum.html#orderedenum
msg227693 - (view) Author: Ram Rachum (cool-RR) * Date: 2014-09-27 16:08
My particular use case is that I have objects with a tuple of enum objects to each, and I want the tuple to be in canonical order rather than random, for convenience.

I can easily use a subclass, but I think it's general enough functionality for it to be included in the standard library.
msg227737 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2014-09-27 21:47
Enums have a definition order to aid in the use-case of auto-numbering, and to make displays consistent.  However, the basic Enum type is unordered.

If you need/want your particular enum type to be ordered, mix-in the ordered magic methods.
History
Date User Action Args
2022-04-11 14:58:08adminsetgithub: 66694
2014-09-27 21:47:30ethan.furmansetstatus: open -> closed
messages: + msg227737

assignee: ethan.furman
resolution: rejected
stage: resolved
2014-09-27 20:43:12ethan.furmansetnosy: + ethan.furman
2014-09-27 16:08:44cool-RRsetmessages: + msg227693
2014-09-27 16:02:47barrysetmessages: + msg227690
2014-09-27 16:00:38barrysetmessages: + msg227688
2014-09-27 15:35:36cool-RRsetmessages: + msg227686
2014-09-27 15:31:11pitrousetnosy: + pitrou
messages: + msg227685
2014-09-27 14:59:24barrysetnosy: + barry
messages: + msg227683
2014-09-27 14:28:23cool-RRcreate