Originally published at https://jpscaletti.com/python-enum-makes-me-angry
Python implementation of Enum screams “design by committee” to me. Enums are supposed to work as replacements for constants in a file or even attributes in a class... why on earth printing an item doesn’t return the value?
class Color(Enum): FRONT = "#ffaf2e" BACK = "#dedede" >>> str(Color.FRONT) 'Color.FRONT' # Are you kidding me!?? >>> str(Color.FRONT.value) '#ffaf2e' # I'm not amused 😒
The worst/best part is that is super easy to fix. Just with a
class PracticalEnum(Enum): def __str__(self): return str(self.value) class Color(PracticalEnum): FRONT = "#ffaf2e" BACK = "#dedede" >>> str(Color.FRONT) '#ffaf2e' # YEAH SCIENCE! 💪
Now we are talking!