[Python-ideas] Should nested classes in an Enum be Enum members?

Ivan Levkivskyi levkivskyi at gmail.com
Sun Jul 1 17:32:38 EDT 2018


On 1 July 2018 at 20:47, Ethan Furman <ethan at stoneleaf.us> wrote:

> On 07/01/2018 06:03 AM, Ivan Levkivskyi wrote:> On 27 June 2018 at 15:46,
> Ethan Furman wrote:
>
> [...]
>>> So I'm asking the community:  What real-world examples can you offer for
>>> either behavior?  Cases where nested
>>> classes should be enum members, and cases where nested classes should
>>> not be members.
>>>
>>
>> I wanted few times to make an enum of enums. For example:
>>
>> class  Method(Enum):
>>      Powell = 1
>>      Newton_CG = 2
>>      <few more>
>>      class Trust(Enum):
>>          Constr = 3
>>          Exact = 4
>>          <few more>
>>
>> So that one can write:
>>
>> minimize(..., method=Method.Powell)
>> minimize(..., method=Method.Trust.Exact)  # this currently fails
>>
>
> In such a case, would you want/expect for
>
> --> list(Method)
>
> to return
>
> [<Powel: 1>, <Newton_CG: 2>, ..., <Trust: -something->]
>
> ?
>

I am fine with what `list(Method)` does currently:

[<Method.Powell: 1>, <Method.Newton_CG: 2>, <Method.Trust: <enum 'Trust'>>]

I think it is intuitive that the nested enums are _not_ automatically
flattened. Also it is easy to write a helper that
manually flattens nested enums, but it would be tedious to group them back
if they are already flattened.

The only thing that I find a bit unintuitive is that one needs to write
`Method.Trust.value.Exact` instead of just
`Method.Trust.Exact`.

Maybe his can be allowed by adding a `__getattr__` to `Method` (by the
metaclass) that will check if a value is an enum and
delegate the attribute access.

--
Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180701/71b5294a/attachment-0001.html>


More information about the Python-ideas mailing list