Skip to content
Advertisement

Representation of all values in Flag enum

I would like to have a “ALL” flag in my python Flags enum for which

JavaScript

holds true. I currently have:

JavaScript

Because this enum might grow at any state of development I would like to have something like

JavaScript

This does not work:

JavaScript

Please note that this question currently only relates to python 3.6 or later.

Advertisement

Answer

There are a few ways to overcome this issue:


One thing to be aware of with the class property method is since the descriptor is defined on the class and not the metaclass the usual protections against setting and deleting are absent — in other words:

JavaScript

Creating a new base class:

JavaScript

And in use:

JavaScript

The interesting difference in the ALL property is the setting of the name in _member_map_ — this allows the same protections afforded to Enum members:

JavaScript

However, there is a race condition here: if RefreshFlag.ALL = ... occurs before RefreshFlag.ALL is activated the first time then it is clobbered; for this reason I would use a decorator in this instance, as the decorator will process the Enum before it can be clobbered.

JavaScript

And in use:

JavaScript
Advertisement