<div dir="ltr">I think the reason they are not supporting __lt__, __gt__,etc. is because ints are optional values for enums, therefore it wouldnt be a good idea to compare enums of different types in that way.<div><br></div>
<div>example:</div><div><br></div><div>>>>class MyEnum(Enum):</div><div>>>>   fir = 1</div><div>>>>   sec = 2</div><div>>>>   thir = "THIRD!"</div><div><br></div><div>and doing </div>
<div><br></div><div>>>> MyEnum.fir >= MyEnum.thir </div><div>would give unexpected results, therefore not making it a great idea</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Apr 12, 2013 at 9:58 AM, R. David Murray <span dir="ltr"><<a href="mailto:rdmurray@bitdance.com" target="_blank">rdmurray@bitdance.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="im">On Fri, 12 Apr 2013 05:55:00 -0700, Eli Bendersky <<a href="mailto:eliben@gmail.com">eliben@gmail.com</a>> wrote:<br>

> Link to the PEP: <a href="http://www.python.org/dev/peps/pep-0435/" target="_blank">http://www.python.org/dev/peps/pep-0435/</a> [it's also pasted<br>
> fully below for convenience].<br>
<br>
</div>This looks great.  There's just one bit I don't understand.  I'm sure<br>
it was discussed in the python-ideas thread, but the discussion of it<br>
in the PEP does not provide any motivation for the decision.<br>
<div class="im"><br>
> The ``Enum`` class supports iteration.  Iteration is defined as the<br>
> sorted order of the item values::<br>
><br>
>     >>> class FiveColors(Enum):<br>
>     ...     pink = 4<br>
>     ...     cyan = 5<br>
>     ...     green = 2<br>
>     ...     blue = 3<br>
>     ...     red = 1<br>
>     >>> [<a href="http://v.name" target="_blank">v.name</a> for v in FiveColors]<br>
>     ['red', 'green', 'blue', 'pink', 'cyan']<br>
<br>
</div>[...]<br>
<div class="im"><br>
> Ordered comparisons between enumeration values are *not* supported.  Enums<br>
> are<br>
> not integers (but see `IntEnum`_ below)::<br>
><br>
>     >>> Colors.red < Colors.blue<br>
>     Traceback (most recent call last):<br>
>     ...<br>
>     NotImplementedError<br>
>     >>> Colors.red <= Colors.blue<br>
>     Traceback (most recent call last):<br>
>     ...<br>
>     NotImplementedError<br>
>     >>> Colors.blue > Colors.green<br>
>     Traceback (most recent call last):<br>
>     ...<br>
>     NotImplementedError<br>
>     >>> Colors.blue >= Colors.green<br>
>     Traceback (most recent call last):<br>
>     ...<br>
>     NotImplementedError<br>
<br>
</div>This is the part that I don't understand.  Enums *clearly* have an<br>
ordering, since the iteration order is defined and stable.  Why should<br>
I not be allowed to compare values from the same Enum type?  There are<br>
certainly use cases where that is very useful.<br>
<br>
To give you a concrete use case: consider response codes from a client<br>
server application constructed the way typical internet protocols are.<br>
We might have:<br>
<br>
    class MyAppCode(Enum):<br>
<br>
        ok = 200<br>
        command_complete = 201<br>
        status_response = 202<br>
        help_response = 203<br>
        service_ready = 204<br>
        signoff_accepted = 205<br>
<br>
        temporary_failure = 400<br>
        service_not_available = 401<br>
        server_error = 402<br>
        out_of_resources = 403<br>
<br>
        error = 500<br>
        syntax_error = 501<br>
        command_not_implemented = 502<br>
        access_denied = 503<br>
        resource_not_found = 504<br>
<br>
<br>
It can be quite handy to be able to say things like:<br>
<br>
    code = myapp.operation(opstring)<br>
    if MyAppCode.temporary_failure < code < MyAppCode.error:<br>
        myframework.requeue(opstring, code=code)<br>
        return False<br>
    elif code > MyAppCode.error:<br>
        raise AppError(code)<br>
    ....<br>
<br>
In talking to an existing internet protocol it would be natural to use<br>
IntEnum and this issue would not arise, but I have recently worked on<br>
an application that had *exactly* the above sort of enumeration used<br>
internally, when it would have been totally appropriate to use Enum rather<br>
than IntEnum.  The ap has several places where an ordered comparison<br>
against the enum is used to check if a code is in the error range or not.<br>
<span class=""><font color="#888888"><br>
--David<br>
</font></span><div class=""><div class="h5">_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/sinistersnare%40gmail.com" target="_blank">http://mail.python.org/mailman/options/python-dev/sinistersnare%40gmail.com</a><br>
</div></div></blockquote></div><br></div></div>