Python enumeration for boolean variable
I am using the enum.Enum class to create a variable with selected members.
The main reason is to allow other developers on my team to use the same convention by choosing one of several allowed members for a variable.
I would like to create a boolean variable in the same way so that other developers can choose True or False.
Is it possible to define an enum that will receive True False parameters? Is there a better alternative?
The following options don't work:
boolean_enum = Enum ('boolean_enum', 'True False')
boolean_enum = Enum ('boolean_enum', True False)
source to share
boolean_enum = Enum('boolean_enum', [('True', True), ('False', False)])
Checkout the documentation for this API: https://docs.python.org/3/library/enum.html#functional-api
If you just specify "True False" for the names parameter, they will be assigned the automatic enumerated values ββ(1,2), which you don't want. And from courase, you cannot just send True False without being a string argument for the names parameter.
so what you want is one of the parameters that allows you to specify a name and value like above.
Edit:
When defined as above, the enum members are not available boolean_enum.True
(but they are accessible via boolean_enum['True']
or boolean_enum(True)
).
To avoid this problem, the field names can be changed and defined as
Enum('boolean_enum', [('TRUE', True), ('FALSE', False)])
Then opens as boolean_enum.True
either boolean_enum['True']
orboolean_enum(True)
source to share