[Python-Dev] PEP 435 -- Adding an Enum type to the Python standard library

Guido van Rossum guido at python.org
Fri Apr 12 23:34:16 CEST 2013

So, pragmatically, if e and f are values of the same enum class,
couldn't e <cmp> f (where <cmp> is any comparison operator) defer to
e.value <cmp> f.value ? Or is the problem with <cmp> being == and e
and f being different enum values with the same underlying value? But
that's already iffy, isn't it? (Barry's favorite solution for
serializing to a database wouldn't work either.) And they could still
be distinguished by using 'is' instead of '=='.

On Fri, Apr 12, 2013 at 2:17 PM, R. David Murray <rdmurray at bitdance.com> wrote:
> On Fri, 12 Apr 2013 14:06:55 -0700, Eli Bendersky <eliben at gmail.com> wrote:
>> I actually think that having values with different types within a single
>> Enum is conceptually wrong and should be disallowed at creation time. With
>> enums, you either care or don't care about their actual value. If you don't
>> care (the most common use case of an enum, IMHO), then no problem here. If
>> you do care, then it's probably for very specific reasons most of which are
>> solved by IntEnum. I can't imagine why someone would need differently typed
>> values in a single enum - this just seems like a completely inappropriate
>> use of an enum to me.
> I'm sure someone will come up with one :)
> But seriously, even if you require all values to be of the same type,
> that doesn't solve the sorting problem:
>>>> class Foo(enum.Enum):
> ...    aa = object()
> ...    bb = object()
> ...
>>>> Foo
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "./enum.py", line 103, in __repr__
>     for k in sorted(cls._enums)))
> TypeError: unorderable types: object() < object()
> Now, you could *further* require that the type of enum values be
> sortable....but that point you really have no excuse for not allowing
> enum values to be compared :)
> --David
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org

--Guido van Rossum (python.org/~guido)

More information about the Python-Dev mailing list