OrderedEnum examples

B

Bas van der Wulp

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?
 
E

Ethan Furman

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

Thanks for catching that, I'll get it fixed asap.

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).

Correct. In 3.4 __ordered__ never came into being as it was not necessary. I added that purely so that 2.x could be
ordered if desired.
 
I

Ian Kelly

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?

The _value attribute was renamed _value_ in:

http://hg.python.org/cpython/rev/511c4daac102

It looks like the example wasn't updated to match. You should
probably just use self.value here since the name of the private
attribute is an implementation detail.
 
E

Ethan Furman

The _value attribute was renamed _value_ in:

http://hg.python.org/cpython/rev/511c4daac102

It looks like the example wasn't updated to match. You should
probably just use self.value here since the name of the private
attribute is an implementation detail.

In `__new__` it has to be `_value_`, but in the other methods `.value` works fine. Updated the 3.4 example with `.value`.
 
B

Bas van der Wulp

In `__new__` it has to be `_value_`, but in the other methods `.value`
works fine. Updated the 3.4 example with `.value`.

That was quick! Thanks Ethan and Ian.

Regards,
Bas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top