Friday Finking: Enum by gum

Rhodri James rhodri at kynesim.co.uk
Fri Jan 24 08:40:25 EST 2020


On 24/01/2020 04:26, DL Neil via Python-list wrote:
> Questions:
> 
> Have I made proper sense of this? (please don't laugh too much)

Um.

My basic take on enums is that they provide a convenient namespace to 
collect together related constants.  API flags and opcodes are an 
obvious example of related constants you would want to keep together, 
but Python enums can be more complex than that both as structured data 
and associated functions.

> Is the above 'interfacing' an appropriate use of enum-s; or is it really 
> 'overkill' or posturing?

I think it's an entirely appropriate use of enums.  In fact it's pretty 
much my only use of them.  One caveat: when you care about the value of 
a constant, set it explicitly.  Don't rely on auto() to come up with the 
right number for you, because the documentation really gives you no 
guarantees.

> Python v3.6+ brings Flag-enums into the game. These seem better for 
> interface use, as described. Do you prefer them to the 'plain-vanilla' 
> enum, and for what reason?

I prefer them when the interface calls for flags, because they behave 
like flags.  I use plain enums when I don't require that behaviour. 
It's horses for courses, as ever.

-- 
Rhodri James *-* Kynesim Ltd


More information about the Python-list mailing list