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 str method:
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!
Hi I’m Juan-Pablo Scaletti
I’m a software writer and open-source creator. This is my corner of the Internet.