Using Python 3, I unpackaged the flufl.enum code into my application source tree just to try it. Sample code:
from taurine.flufl.enum import Enum
class Colors(Enum):
red = 1
green = 2
blue = 3
print(Colors.red)
red = Colors.red
print("red == Colors.red "+str(red == Colors.red))
print("red == Colors.blue "+str(red == Colors.blue))
print("red is Colors.red "+str(red is Colors.red))
Everything works as expected except the print(Color.red). According to http://packages.python.org/flufl.enum/docs/using.html I’d expect it to print “Colors.red” but it’s printing 1. Anyone familiar with this package know if there’s a way to get it to print “Colors.red”? I’ve posted a question on the library’s site but thought someone here might have experience as well.
EDIT: It does work as expected if I define Colors with:
Colors = make_enum('Colors','red green blue')
But I prefer the syntax of:
class Colors(Enum):
red = 1
green = 2
blue = 3
I realized init wasn’t even being called on EnumMetaclass. I think the following code in _enum.py is meant to make inheriting from Enum all you’d need to do, but something about it doesn’t work and it’s beyond me:
This works:
I’m happy now.
EDIT: Found out why. See the first answer to the following question: Shouldn't __metaclass__ force the use of a metaclass in Python?