Andres Ayala <killer...@gmail.com> added the comment:

I see, with mixed types you need to use __new__ to construct the elements (I 
imagine is specially important for non mutable types)

I have modified the example of the coordinates to try to use a mixed type. Is 
not the most useful thing, but it mix the bytes class.
Is it not obvious how to correctly use the mixin + __new__ operator so it is 
easy that the example is not correct or can be done better.

class Coordinate(bytes, Enum):
    """
    Coordinate with binary codes that can be indexed by the int code.
    """
    def __new__(cls, value, label, unit):
        obj = bytes.__new__(cls, [value])
        obj._value_ = value
        obj.label = label
        obj.unit = unit
        return obj

    PX = (0, 'P.X', 'km')
    PY = (1, 'P.Y', 'km')
    VX = (2, 'V.X', 'km/s')
    VY = (3, 'V.Y', 'km/s')


# This works as expected
for element in Coordinate:
    print("{0}: {0.label}[{0.unit}] = {0.value}".format(element))

# And this Work
print("Obtain P.Y from the name:", Coordinate['PY'])
print("Obtain V.Y from the index:", Coordinate(3))

# This shall be False
print('Coordinate.PY == 1 is:', Coordinate.PY == 1)
# But this shall be True
print("Coordinate.PY == b'\\01' is:", Coordinate.PY == b'\01')

Thanks!

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33437>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to