I just started using Python for server-side programming (coming from TypeScript) and the verbosity of typing .value
for an Enum is making me consider not using it at all.
In TypeScript the value of an Enum is retrieved like so..
enum CompassDirection {
North = "North",
}
// I do not have to ask for the value
const isEqual = CompassDirection.North === "North" ? true : false
console.log(isEqual) // returns true
But in Python I believe the only way to retrieve the value is using .value
like so..
from enum import Enum
class CompassDirection(Enum):
NORTH = 'NORTH'
isEqual = CompassDirection.NORTH == 'NORTH'
print(isEqual) // false
isEqual = CompassDirection.NORTH.value == 'NORTH'
print(isEqual) // true
My purpose of using Enum was for inference and so I wouldn't have to repeatedly type strings such as "NORTH" all in my code.
If I made a function I would have to call it everywhere as well - e.g.
def get_enum_value(enum):
return enum.value
# Calling this every time I want to use the enum.
get_enum_value(CompassDirection.NORTH)
get_enum_value(XEnum.Value)
get_enum_value(YEnum.Value)
Yet if I do not do this, this I'm repeatedly typing .value
everywhere, which I'm finding counter-intuitive maybe because of my TypeScript bias.
Is there a way I can use Enum or a Dictionary achieve the inference that I want, keep my code as DRY as possible so I can avoiding typing my strings?
Note:
I don't mind making a few constants but I could see my imports getting lengthy for code-splitting / re-use.
Am I missing something? I'd like to understand this as soon as possible.