(Note) The title is just a story, not a Python diss. I rather love it.
Introduced in Python 3.4, enums can have instance variables and methods. You no longer have to envy Java enums.
Enum with method
from enum import Enum
class JavaLikeEnum(Enum):
foo = ('foo_name', 50)
bar = ('bar_name', 100)
def __init__(self, item_name, price):
self.item_name = item_name
self.price = price
def with_tax(self):
return self.price * 1.08
print(JavaLikeEnum.foo.with_tax())
print(JavaLikeEnum.bar.with_tax())
The package for backport is provided by pip, so Python 3.3 or earlier is fine. Let's use it hard from now on.
python
% pip search enum34
enum34 - Python 3.4 Enum backported to 3.3, 3.2, 3.1, 2.7, 2.6, 2.5, and 2.4