Enums: making a single enum

Peter Otten __peter__ at web.de
Sat May 26 08:24:39 EDT 2018


Steven D'Aprano 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?

I think all three states should be created equal. Would a full-blown

>>> class State(enum.IntEnum):
...     false = False
...     true = True
...     maybe = 2
... 
>>> State.maybe
<State.maybe: 2>
>>> bool(State.false)
False
>>> State.true == True
True
>>> list(State)
[<State.false: 0>, <State.true: 1>, <State.maybe: 2>]
>>> State(True)
<State.true: 1>

complicate your actual code?




More information about the Python-list mailing list