In Python, I've been creating enums using the enum module. Usually with the int-version to allow conversion:
from enum import IntEnum
class Level(IntEnum):
DEFAULTS = 0
PROJECT = 1
MASTER = 2
COLLECT = 3
OBJECT = 4
I would like to provide some type of invalid or undefined value for variables of this type. For example, if an object is initialized and its level is currently unknown, I would like to create it by doing something like self.level = Level.UNKNOWN
or perhaps Level.INVALID
or Level.NONE
. I usually set the internal value of these special values to -1
.
The type of problems I keep running into is that adding any special values like this will break iteration and len()
calls. Such as if I wanted to generate some list to hold each level type list = [x] * len(Level)
, it would add these extra values to the list length, unless I manually subtract 1 from it. Or if I iterated the level types for lvl in Level:
, I would have to manually skip over these special values.
So I'm wondering if there is any clever way to fix this problem? Is it pointless to even create an invalid value like this in Python? Should I just be using something like the global None
instead? Or is there some way to define the invalid
representation of the enumerator so that it doesn't get included in iteration or length logic?