Message316283
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! |
|
Date |
User |
Action |
Args |
2018-05-08 10:24:07 | killerrex | set | recipients:
+ killerrex, barry, eli.bendersky, ethan.furman |
2018-05-08 10:24:07 | killerrex | set | messageid: <1525775047.16.0.682650639539.issue33437@psf.upfronthosting.co.za> |
2018-05-08 10:24:07 | killerrex | link | issue33437 messages |
2018-05-08 10:24:06 | killerrex | create | |
|