On 10/22/2019 03:13 PM, Steve Jorgensen wrote:
Ethan Furman wrote:

Experimenting is good!  However, you'll want to either build your own metaclass
and/or prepared dict, or do some work on your __new__/__init__
methods for building enum members.  Currently, you are reassigning _value_ in
__init__, which leaves some internal structures not matching the Enum.
--> class Food(ChoiceEnum):
...     APPLE = ()
...     ICED_TEA = ()
...
--> Food['APPLE']
<Food.APPLE: 'APPLE'>
--> Food.APPLE
<Food.APPLE: 'APPLE'>
--> Food('APPLE')
Traceback (most recent call last):
    ...
ValueError: 'APPLE' is not a valid Food

Thanks for that info.

Per the example in 
https://docs.python.org/3/library/enum.html#when-to-use-new-vs-init, it looks 
like I can properly set the `_value_` property in the `__new__` method of the 
`Enum` subclass without needing to subclass `EnumMeta`. Am I understanding that 
correctly?

Yes, you are.  The fun part for you is that the value can sometimes be the 
name, and the name is not passed into `__new__`.

The name /is/ passed into `_generate_next_value_`, but if any value is supplied 
then `_generate_next_value_` isn't called.

I haven't had enough space cycles to either find a solution or rule any out, 
but I know the limitations above are baked into EnumMeta and _EnumDict, so if 
you rolled your own you could simply not put them in.

--
~Ethan~
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/7A2O4HOI7LR47LEBVABGS2R6YF77E7FT/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to