Enums: making a single enum

Serhiy Storchaka storchaka at gmail.com
Sat May 26 06:52:33 EDT 2018


26.05.18 08:07, Ian Kelly пише:
> On Fri, May 25, 2018 at 11:00 PM, Chris Angelico <rosuav at gmail.com> wrote:
>> On Sat, May 26, 2018 at 2:46 PM, Steven D'Aprano
>> <steve+comp.lang.python at pearwood.info> wrote:
>>> Am I silly for wanting to make a single enum?
>>>
>>> I have a three-state flag, True, False or Maybe. Is is confusing or bad
>>> practice to make a single enum for the Maybe case?
>>>
>>>
>>> from enum import Enum
>>> class State(Enum):
>>>      Maybe = 2
>>>
>>> Maybe = State.Maybe
>>> del State
>>>
>>> Is there a better way of handling a three-state flag like this?
>>>
>>
>> Does it need to have a value of 2? If not:
>>
>> # Tri-state logic
>> Maybe = object()
> 
> The enum has a nice __str__ though.

Not very nice. It contains a reference to non-existing class State. The 
repr contains also the unrelevant here value of 2.

If you need just an identifiable singleton and don't bother about the 
exact representation, then a list can be enough:

Maybe = ['Maybe']




More information about the Python-list mailing list