OrderedEnum examples

Bas van der Wulp Bas.vdWulp at gmail.com
Tue Jul 30 20:18:16 CEST 2013


Using the enum34 0.9.13 package from PyPi in Python 2.7.3, the examples 
for OrderedEnum seem to be broken.

The example in the package documentation reads:

class OrderedEnum(Enum):
     def __ge__(self, other):
         if self.__class__ is other.__class__:
             return self._value >= other._value
         return NotImplemented
     def __gt__(self, other):
         if self.__class__ is other.__class__:
             return self._value > other._value
         return NotImplemented
     def __le__(self, other):
         if self.__class__ is other.__class__:
             return self._value <= other._value
         return NotImplemented
     def __lt__(self, other):
         if self.__class__ is other.__class__:
             return self._value < other._value
         return NotImplemented

class Grade(OrderedEnum):
     __ordered__ = 'A B C D F'
     A = 5
     B = 4
     C = 3
     D = 2
     F = 1

Grade.C < Grade.A

to which Python replies with:

Traceback (most recent call last):
   File "test.py", line 35, in <module>
     print Grade.C < Grade.A
   File "test.py", line 23, in __lt__
     return self._value < other._value
AttributeError: 'Grade' object has no attribute '_value'

Also, in the example in the Python 3.4 library documentation (section 
8.17.2) has the __ordered__ attribute removed (presumably because, in 
contrast to Python 2.x, Python 3 will respect the order of attribute 
definition). This example gives the same ValueErrror when using the 
enum34 package in Python 2.7.3. It is the same example, after all.

Replacing each occurrence of self._value with either self._value_ or 
self.value in the examples seems to make them work as expected.

Are both examples incorrect, or not intended to work in Python 2.x?

--
S. van der Wulp



More information about the Python-list mailing list