I understand the sense of using an Enum
when it's converting a human-readable string into an underlying (e.g. numeric) value, with the FederalHoliday
class in this answer being a good example of that.
But the use-case I'm considering is just where a function parameter is restricted to a set of possible values, which are currently passed as "magic strings". So implementing an Enum
here wouldn't really improve code readability (if anything, it would make the code more cluttered). And turning a string into an Enum
, only to compare on what is effectively the same string (i.e. the name of the Enum
) feels like overkill.
This answer has a fantastic list of general advantages of enums, but I'm not sure how many of them apply in this case.
To clarify the case I'm meaning, there is an example of it here, with a function that prints a string with certain capitalization as specified by mode
:
def print_my_string(my_string, mode):
if mode == 'l':
print(my_string.lower())
elif mode == 'u':
print(my_string.upper())
elif mode == 'c':
print(my_string.capitalize())
else:
raise ValueError("Unrecognised mode")
To see this in action, running this:
for mode in ['l', 'u', 'c']:
print_my_string("RaNdoM CAse StRING", mode)
gives:
random case string
RANDOM CASE STRING
Random case string
So my question is:
What advantage does an Enum
bring when the strings don't represent another value underneath? Is there any way that it makes the code more robust? Does it add anything?
Other things I've read:
Mainly about Enum
s in general, especially when the strings represent another value underneath:
This seems to have an example similar to mine (under Enums are interoperable), but I struggled to understand the technical discussion around it, and it only shows setting the Enum
, not using it: